Add ability to get current event loop within asyncio coroutine
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
Can you explain why asyncio.get_event_loop() doesn't do what you want?
On Thu, May 19, 2016 at 11:49 AM, Ilya Kulakov
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
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
-- --Guido van Rossum (python.org/~guido)
On Thu, May 19, 2016 at 12:49 PM, Ilya Kulakov
Current implementation of `get_event_loop` method is not sufficient, it will not work if thread has more than one event loop.
Why do you need more than one event loop per thread? You couldn't run more than one of them at a time.
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).
To me this sounds like a case for a custom event loop policy, which is what determines the behavior of get_event_loop.
participants (3)
-
Guido van Rossum
-
Ian Kelly
-
Ilya Kulakov