Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

Commit 03016ee

Browse files
committed
try async generators
1 parent 9acdceb commit 03016ee

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

Diff for: asyncio/run.py

+21-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
__all__ = ['run', 'forever']
44

5+
import inspect
56
import threading
67

78
from . import coroutines
@@ -67,8 +68,8 @@ async def main():
6768
if not isinstance(threading.current_thread(), threading._MainThread):
6869
raise RuntimeError(
6970
"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))
7273

7374
loop = events.new_event_loop()
7475
try:
@@ -77,15 +78,26 @@ async def main():
7778
if debug:
7879
loop.set_debug(True)
7980

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]
8298

83-
try:
84-
loop.run_forever()
85-
except BaseException as ex:
86-
result = loop.run_until_complete(task)
8799
else:
88-
result = task.result()
100+
result = loop.run_until_complete(coro)
89101

90102
try:
91103
# `shutdown_asyncgens` was added in Python 3.6; not all

0 commit comments

Comments
 (0)