2
2
3
3
__all__ = ['run' , 'forever' ]
4
4
5
+ import inspect
5
6
import threading
6
7
7
8
from . import coroutines
@@ -67,8 +68,8 @@ async def main():
67
68
if not isinstance (threading .current_thread (), threading ._MainThread ):
68
69
raise RuntimeError (
69
70
"asyncio.run() must be called from the main thread" )
70
- if not coroutines .iscoroutine (coro ):
71
- raise ValueError ("a coroutine was expected, got {!r}" .format (coro ))
71
+ # if not coroutines.iscoroutine(coro):
72
+ # raise ValueError("a coroutine was expected, got {!r}".format(coro))
72
73
73
74
loop = events .new_event_loop ()
74
75
try :
@@ -77,15 +78,26 @@ async def main():
77
78
if debug :
78
79
loop .set_debug (True )
79
80
80
- task = loop .create_task (coro )
81
- task .add_done_callback (lambda task : loop .stop ())
81
+ if inspect .isasyncgen (coro ):
82
+ result = None
83
+ loop .run_until_complete (coro .asend (None ))
84
+ try :
85
+ loop .run_forever ()
86
+ except BaseException as ex :
87
+ try :
88
+ loop .run_until_complete (coro .athrow (ex ))
89
+ except StopAsyncIteration as ex :
90
+ if ex .args :
91
+ result = ex .args [0 ]
92
+ else :
93
+ try :
94
+ loop .run_until_complete (coro .asend (None ))
95
+ except StopAsyncIteration as ex :
96
+ if ex .args :
97
+ result = ex .args [0 ]
82
98
83
- try :
84
- loop .run_forever ()
85
- except BaseException as ex :
86
- result = loop .run_until_complete (task )
87
99
else :
88
- result = task . result ( )
100
+ result = loop . run_until_complete ( coro )
89
101
90
102
try :
91
103
# `shutdown_asyncgens` was added in Python 3.6; not all
0 commit comments