[Python-ideas] Built-in function to run coroutines

Ivan Levkivskyi levkivskyi at gmail.com
Sat Nov 12 15:37:32 EST 2016


Hi,

async/await syntax is a very nice recent feature, but there is something
that I miss for coroutines defined with async def, as compared to
generators. Coroutines represent an interesting mental model that goes
beyond only asynchronous IO, so that I play with them in REPL often. But
there is no built-in function to actually run a coroutine, so that
typically I use something like:

>>> def run(coro):
...     try:
...         coro.send(None)
...     except StopIteration as e:
...         return e.value

>>> async def f():
...     return 42

>>> run(f())
42

There is a simple yet useful function for interactive play with generators
- ``next``, but not for coroutines. There is an option to do:

>>> import asyncio
>>> loop = asyncio.get_event_loop()
>>> loop.run_until_complete(f())
42

But this feels a bit redundant for an interactive play. I would propose to
add something like an above described ``run`` function to built-ins.

Yes, I know, there is a very high bar for adding a built-in function, but I
believe such a function will help to promote async/await to a wider
community (especially to novices).

--
Ivan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161112/161affba/attachment.html>


More information about the Python-ideas mailing list