8000 Add asyncio.run() and asyncio.run_forever() functions. by 1st1 · Pull Request #465 · python/asyncio · GitHub
[go: up one dir, main page]

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

Add asyncio.run() and asyncio.run_forever() functions. #465

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
try async generators
  • Loading branch information
1st1 committed Nov 15, 2016
commit f24ff3056ce9506390594d94fd627b77964b97e0
14 changes: 0 additions & 14 deletions asyncio/base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,6 @@ def __init__(self):
# Set to True when `loop.shutdown_asyncgens` is called.
self._asyncgens_shutdown_called = False

# Future that isn't resolved while the loop is running.
self._forever_fut = None

def __repr__(self):
return ('<%s running=%s closed=%s debug=%s>'
% (self.__class__.__name__, self.is_running(),
Expand Down Expand Up @@ -433,12 +430,8 @@ def shutdown_asyncgens(self):
'asyncgen': agen
})

def get_forever_future(self):
return self._forever_fut

def run_forever(self):
"""Run until stop() is called."""
self._forever_fut = self.create_future()
self._check_closed()
if self.is_running():
raise RuntimeError('This event loop is already running')
Expand All @@ -457,14 +450,7 @@ def run_forever(self):
self._run_once()
if self._stopping:
break
except BaseException as ex:
self._forever_fut.set_exception(ex)
self._forever_fut._log_traceback = False
raise ex
else:
self._forever_fut.set_result(None)
finally:
self._forever_fut = None
self._stopping = False
self._thread_id = None
events._set_running_loop(None)
Expand Down
3 changes: 0 additions & 3 deletions asyncio/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,6 @@ def get_debug(self):
def set_debug(self, enabled):
raise NotImplementedError

def get_forever_future(self):
raise NotImplementedError


class AbstractEventLoopPolicy:
"""Abstract policy for accessing the event loop."""
Expand Down
38 changes: 29 additions & 9 deletions asyncio/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

__all__ = ['run', 'forever']

import inspect
import threading

from . import coroutines
from . import events


def _isasyncgen(obj):
if hasattr(inspect, 'isasyncgen'):
return inspect.isasyncgen(obj)
return False


@coroutines.coroutine
def forever():
"""Wait until the current event loop stops running.
Expand Down Expand Up @@ -67,8 +74,10 @@ 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) and not _isasyncgen(coro):
raise ValueError(
"a coroutine or an asynchronous generator was expected, "
"got {!r}".format(coro))

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

task = loop.create_task(coro)
task.add_done_callback(lambda task: loop.stop())
if _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