Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
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
23 changes: 20 additions & 3 deletions Lib/asyncio/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import contextvars
import enum
import functools
import inspect
import threading
import signal
from . import coroutines
Expand Down Expand Up @@ -84,9 +85,14 @@ def get_loop(self):
return self._loop

def run(self, coro, *, context=None):
"""Run a coroutine inside the embedded event loop."""
if not coroutines.iscoroutine(coro):
raise ValueError("a coroutine was expected, got {!r}".format(coro))
"""Run code in the embedded event loop.

The argument can be any awaitable object.

If the argument is a coroutine, it is wrapped in a Task.

Return the awaitable's result or raise an exception.
"""

if events._get_running_loop() is not None:
# fail fast with short traceback
Expand All @@ -95,8 +101,19 @@ def run(self, coro, *, context=None):

self._lazy_init()

if not coroutines.iscoroutine(coro):
if inspect.isawaitable(coro):
async def _wrap_awaitable(awaitable):
return await awaitable

coro = _wrap_awaitable(coro)
else:
raise TypeError('An asyncio.Future, a coroutine or an '
'awaitable is required')

if context is None:
context = self._context

task = self._loop.create_task(coro, context=context)

if (threading.current_thread() is threading.main_thread()
Expand Down
29 changes: 19 additions & 10 deletions Lib/test/test_asyncio/test_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ async def main():
def test_asyncio_run_only_coro(self):
for o in {1, lambda: None}:
with self.subTest(obj=o), \
self.assertRaisesRegex(ValueError,
'a coroutine was expected'):
self.assertRaisesRegex(TypeError,
'an awaitable is required'):
asyncio.run(o)

def test_asyncio_run_debug(self):
Expand Down Expand Up @@ -319,19 +319,28 @@ async def f():
def test_run_non_coro(self):
with asyncio.Runner() as runner:
with self.assertRaisesRegex(
ValueError,
"a coroutine was expected"
TypeError,
"an awaitable is required"
):
runner.run(123)

def test_run_future(self):
with asyncio.Runner() as runner:
with self.assertRaisesRegex(
ValueError,
"a coroutine was expected"
):
fut = runner.get_loop().create_future()
runner.run(fut)
fut = runner.get_loop().create_future()
fut.set_result('done')
self.assertEqual('done', runner.run(fut))

def test_run_awaitable(self):
class MyAwaitable:
def __await__(self):
return self.run().__await__()

@staticmethod
async def run():
return 'done'

with asyncio.Runner() as runner:
self.assertEqual('done', runner.run(MyAwaitable()))

def test_explicit_close(self):
runner = asyncio.Runner()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Allow :meth:`asyncio.Runner.run` to accept :term:`awaitable`
objects instead of simply :term:`coroutine`\s.
Loading