-
Notifications
You must be signed in to change notification settings - Fork 589
/
asyncio.py
54 lines (48 loc) 路 1.77 KB
/
asyncio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import asyncio
import contextlib
import sys
@contextlib.contextmanager
def with_event_loop(event_loop=None):
loop_previous = asyncio.get_event_loop()
if event_loop is None:
loop_new = asyncio.new_event_loop()
else:
loop_new = event_loop
asyncio.set_event_loop(loop_new)
# private API, but present from CPython 3.3ish-3.10+
asyncio.events._set_running_loop(None)
try:
yield
finally:
asyncio.set_event_loop(loop_previous)
def check_ipython():
IPython = sys.modules.get('IPython')
if IPython:
IPython_version = tuple(map(int, IPython.__version__.split('.')[:3]))
if IPython_version < (7, 0, 0):
raise RuntimeError(f'You are using IPython {IPython.__version__} while we require 7.0.0, please update IPython')
def check_patch_tornado():
'''If tornado is imported, add the patched asyncio.Future to its tuple of acceptable Futures'''
if 'tornado' in sys.modules:
import tornado.concurrent
if asyncio.Future not in tornado.concurrent.FUTURES:
tornado.concurrent.FUTURES = tornado.concurrent.FUTURES + (asyncio.Future, )
def just_run(coro):
'''Make the coroutine run, even if there is already an event loop running (using nest_asyncio)'''
try:
loop = asyncio.get_event_loop()
had_loop = True
except RuntimeError:
had_loop = False
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
if had_loop:
check_ipython()
import nest_asyncio
nest_asyncio.apply()
check_patch_tornado()
return loop.run_until_complete(coro)
finally:
if not had_loop: # remove loop if we did not have one
asyncio.set_event_loop(None)