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

Commit

Permalink
Use async generators?
Browse files Browse the repository at this point in the history
  • Loading branch information
1st1 committed Nov 15, 2016
1 parent 03016ee commit a6a4c1d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 20 deletions.
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
14 changes: 11 additions & 3 deletions asyncio/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
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 @@ -68,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 @@ -78,7 +86,7 @@ async def main():
if debug:
loop.set_debug(True)

if inspect.isasyncgen(coro):
if _isasyncgen(coro):
result = None
loop.run_until_complete(coro.asend(None))
try:
Expand Down

0 comments on commit a6a4c1d

Please sign in to comment.