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
Fix few nits; add a test.
  • Loading branch information
1st1 committed Nov 17, 2016
commit 275072a2fbae0c98619597536d85a65bd72d706b
2 changes: 1 addition & 1 deletion asyncio/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def run(main, *, debug=False):
asyncio programs, and should not be used to call asynchronous
APIs.

Example::
Example:

async def main():
await asyncio.sleep(1)
Expand Down
20 changes: 19 additions & 1 deletion tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ async def main():
with self.assertRaisesRegex(RuntimeError, 'one empty yield'):
self.assertIsNone(asyncio.run_forever(main()))

def test_asyncio_run_forever_try_finally(self):
DONE = 0

async def main():
nonlocal DONE
self.stop_soon()
try:
yield
finally:
DONE += 1

asyncio.run_forever(main())
self.assertEqual(DONE, 1)

def test_asyncio_run_forever_raises_before_yield(self):
async def main():
await asyncio.sleep(0)
Expand Down Expand Up @@ -192,14 +206,18 @@ def test_asyncio_run_forever_base_exception(self):
raise unittest.SkipTest(
'this test requires Python 3.6b4 or greater')

DONE = 0

class MyExc(BaseException):
pass

async def main():
nonlocal DONE
self.stop_soon(exc=MyExc)
try:
yield
except MyExc:
pass
DONE += 1

asyncio.run_forever(main())
self.assertEqual(DONE, 1)
0