diff options
author | Ron Frederick <ronf@timeheart.net> | 2024-09-26 06:15:08 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-26 06:15:08 (GMT) |
commit | 1229cb8c1412d37cf3206eab407f03e21d602cbd (patch) | |
tree | 9ff9363d0b2c15026c225cb16e6305fe8cd9028c /Lib/asyncio | |
parent | 46f5cbca4c37c57f718d3de0d7f7ddfc44298535 (diff) | |
download | cpython-1229cb8c1412d37cf3206eab407f03e21d602cbd.zip cpython-1229cb8c1412d37cf3206eab407f03e21d602cbd.tar.gz cpython-1229cb8c1412d37cf3206eab407f03e21d602cbd.tar.bz2 |
gh-120284: Enhance `asyncio.run` to accept awaitable objects (#120566)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Diffstat (limited to 'Lib/asyncio')
-rw-r--r-- | Lib/asyncio/runners.py | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/Lib/asyncio/runners.py b/Lib/asyncio/runners.py index 1b89236..0e63c34 100644 --- a/Lib/asyncio/runners.py +++ b/Lib/asyncio/runners.py @@ -3,6 +3,7 @@ __all__ = ('Runner', 'run') import contextvars import enum import functools +import inspect import threading import signal from . import coroutines @@ -84,10 +85,7 @@ class Runner: 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.""" if events._get_running_loop() is not None: # fail fast with short traceback raise RuntimeError( @@ -95,8 +93,19 @@ class Runner: 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() |