-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to skip a given number of awaits and wait for the next await. Refs #64
- Loading branch information
Showing
3 changed files
with
121 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,31 @@ | ||
# coding: utf-8 | ||
import asyncio | ||
import contextlib | ||
|
||
|
||
def run_coroutine(coroutine): | ||
loop = asyncio.new_event_loop() | ||
def run_coroutine(coroutine, loop=None): | ||
if loop is None: | ||
loop = asyncio.new_event_loop() | ||
close = True | ||
else: | ||
close = False | ||
|
||
with replace_loop(loop, close): | ||
return loop.run_until_complete(coroutine) | ||
|
||
|
||
@contextlib.contextmanager | ||
def replace_loop(loop, close=True): | ||
try: | ||
current_loop = asyncio.get_event_loop() | ||
except: | ||
current_loop = None | ||
|
||
asyncio.set_event_loop(loop) | ||
try: | ||
return loop.run_until_complete(coroutine) | ||
yield | ||
finally: | ||
loop.close() | ||
if close: | ||
loop.close() | ||
|
||
asyncio.set_event_loop(current_loop) |