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
Address Ivan's review
  • Loading branch information
1st1 committed Nov 17, 2016
commit 7e67b4859bf3e9f8e8ebb4c246dece916a9e8a98
8 changes: 5 additions & 3 deletions asyncio/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,13 @@ async def main():
"asyncio.run_forever() cannot be called from a running event loop")
if not isinstance(threading.current_thread(), threading._MainThread):
raise RuntimeError(
"asyncio.run() must be called from the main thread")
"asyncio.run_forever() must be called from the main thread")
if not inspect.isasyncgen(main):
raise ValueError(
"an asynchronous generator was expected, got {!r}".format(main))

one_yield_msg = ("asyncio.run_forever() supports only "
"asynchronous generators with one empty yield")
loop = events.new_event_loop()
try:
events.set_event_loop(loop)
Expand All @@ -119,7 +121,7 @@ async def main():
except StopAsyncIteration as ex:
return
if ret is not None:
raise RuntimeError("only empty yield is supported")
raise RuntimeError(one_yield_msg)

yielded_twice = False
try:
Expand All @@ -140,7 +142,7 @@ async def main():
yielded_twice = True

if yielded_twice:
raise RuntimeError("only one yield is supported")
raise RuntimeError(one_yield_msg)

finally:
_cleanup(loop)
4 changes: 2 additions & 2 deletions tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def test_asyncio_run_forever_non_none_yield(self):
async def main():
yield 1

with self.assertRaisesRegex(RuntimeError, 'only empty'):
with self.assertRaisesRegex(RuntimeError, 'one empty yield'):
self.assertIsNone(asyncio.run_forever(main()))

def test_asyncio_run_forever_raises_before_yield(self):
Expand Down Expand Up @@ -152,7 +152,7 @@ async def main():
yield
raise ValueError('spam')

with self.assertRaisesRegex(RuntimeError, 'only one yield'):
with self.assertRaisesRegex(RuntimeError, 'one empty yield'):
asyncio.run_forever(main())

def test_asyncio_run_forever_only_ag(self):
Expand Down
0