[Python-ideas] Add ability to get current event loop within asyncio coroutine

Ilya Kulakov kulakov.ilya at gmail.com
Thu May 19 14:49:52 EDT 2016


asyncio is a great library which with recent changes in 3.5 was made even better.

However there is an inconvenience that bothers me and looks very unnatural: inability to access event loop
that executes a coroutine from within that coroutine.

This is unnatural, because we have `self` and `cls` to access object from a method it is bound to,
we have `current_thread` to get an instance of Thread that currently executes code, but for a coroutine
we have no such method, we cannot get its context of execution.
Current implementation of `get_event_loop` method is not sufficient, it will not work if thread has more
than one event loop.

I think Python would benefit from providing either a new method (e.g. `asyncio.current_event_loop`) or modifying `asyncio.get_event_loop`
to return current event loop when called from a coroutine. It should, in my opinion, reduce necessity of passing event loop
between coroutines in application's code as it is done in asyncio itself and 3rd party libraries (like aiohttp).


Best Regards,
Ilya Kulakov

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160519/a7721c37/attachment.html>


More information about the Python-ideas mailing list