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

Commit

Permalink
try async generators
Browse files Browse the repository at this point in the history
  • Loading branch information
1st1 committed Nov 15, 2016
1 parent 9acdceb commit 03016ee
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions asyncio/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

__all__ = ['run', 'forever']

import inspect
import threading

from . import coroutines
Expand Down Expand Up @@ -67,8 +68,8 @@ async def main():
if not isinstance(threading.current_thread(), threading._MainThread):
raise RuntimeError(
"asyncio.run() must be called from the main thread")
if not coroutines.iscoroutine(coro):
raise ValueError("a coroutine was expected, got {!r}".format(coro))
# if not coroutines.iscoroutine(coro):
# raise ValueError("a coroutine was expected, got {!r}".format(coro))

loop = events.new_event_loop()
try:
Expand All @@ -77,15 +78,26 @@ async def main():
if debug:
loop.set_debug(True)

task = loop.create_task(coro)
task.add_done_callback(lambda task: loop.stop())
if inspect.isasyncgen(coro):
result = None
loop.run_until_complete(coro.asend(None))
try:
loop.run_forever()
except BaseException as ex:
try:
loop.run_until_complete(coro.athrow(ex))
except StopAsyncIteration as ex:
if ex.args:
result = ex.args[0]
else:
try:
loop.run_until_complete(coro.asend(None))
except StopAsyncIteration as ex:
if ex.args:
result = ex.args[0]

try:
loop.run_forever()
except BaseException as ex:
result = loop.run_until_complete(task)
else:
result = task.result()
result = loop.run_until_complete(coro)

try:
# `shutdown_asyncgens` was added in Python 3.6; not all
Expand Down

0 comments on commit 03016ee

Please sign in to comment.