-
-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathpydevd_async_utils.py
More file actions
45 lines (29 loc) · 939 Bytes
/
Copy pathpydevd_async_utils.py
File metadata and controls
45 lines (29 loc) · 939 Bytes
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
__all__ = ["eval_async_coro"]
import types
def _get_current_loop():
import asyncio
try:
return asyncio.get_running_loop()
except (RuntimeError, AttributeError):
return asyncio.new_event_loop()
def _prepare_coro(coro, _locals, _globals):
if isinstance(coro, types.CodeType):
return eval(coro, _locals, _globals)
return coro
def eval_async_coro(coro, _locals, _globals):
import asyncio
coro = _prepare_coro(coro, _locals, _globals)
loop = _get_current_loop()
if not loop.is_running():
return loop.run_until_complete(coro)
current = asyncio.current_task(loop)
t = loop.create_task(coro)
try:
if current is not None:
asyncio._leave_task(loop, current)
while not t.done():
loop._run_once()
return t.result()
finally:
if current is not None:
asyncio._enter_task(loop, current)