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

On Thu, May 19, 2016 at 11:49 AM, Ilya Kulakov <kulakov.ilya@gmail.com> wrote:
I was about to make the same proposal. I can add two practical arguments in favor. First, some coroutines could be simplified. For instance, asyncio.sleep takes an optional loop argument, defaulting to get_event_loop(). But get_event_loop is called inside the coroutine, and therefore inside a running event loop. This loop is actually the only valid choice, and using anything else should raise an exception. If get_event_loop is guaranteed to return the current running loop, then there is no reason to pass it as an argument to asyncio.sleep. Other coroutines could benefit from that, including wait and wait_for. It would also help for queues, locks and other task functions (gather, shield, as_completed, etc.). Here the loop argument is still useful, in case those objects are declared outside their event loop. However, it would be really convenient to assume loop=None always works as expected if the object is created inside a coroutine. In many cases, libraries tend to forward the loop argument from coroutine to coroutine, just in case someone writes: loop = asyncio.new_event_loop() loop.run_until_complete(my_coro(loop=loop)) instead of: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) loop.run_until_complete(my_coro()) TL;DR `async def coro(*, loop=None)` doesn't really make sense, and could be discouraged if get_event_loop was guaranteed to return the running event loop when called inside a coroutine.

OK, I finally see the point. There are legitimate situations where get_event_loop() returns None (this is how most of asyncio's tests are running). So we want a separate function, e.g. get_current_event_loop(). Send a PR to the GitHub project. On Fri, May 20, 2016 at 2:02 AM, Vincent Michel <vxgmichel@gmail.com> wrote:
-- --Guido van Rossum (python.org/~guido)

Unless there are tests that expect that get_event_loop() would return None when called from within coroutine it should be fine.
So we want a separate function, e.g. get_current_event_loop(). Send a PR to the GitHub project.
Also see related bug: http://bugs.python.org/issue26969 <http://bugs.python.org/issue26969> Yuri did some work, but he argumented in favor of modifying get_event_loop. Should we continue discussion of implementation here or in that issue?

On Fri, May 20, 2016 at 10:53 AM, Ilya Kulakov <kulakov.ilya@gmail.com> wrote:
The tests are supposed to fail if something doesn't explicitly pass the loop; this is how we test that all code in asyncio itself always correctly passes the loop rather than accidentally relying on get_event_loop().
I consider it a feature that you can make get_event_loop() return None. But I think it would be a bug (in the Policy class) if it returned some other loop that's not the one that's running. I also think it's fine for a particular library or app to request that get_event_loop() not return None. User code is simpler that way. -- --Guido van Rossum (python.org/~guido)

OK, I finally see the point. There are legitimate situations where get_event_loop() returns None (this is how most of asyncio's tests are running). So we want a separate function, e.g. get_current_event_loop(). Send a PR to the GitHub project. On Fri, May 20, 2016 at 2:02 AM, Vincent Michel <vxgmichel@gmail.com> wrote:
-- --Guido van Rossum (python.org/~guido)

Unless there are tests that expect that get_event_loop() would return None when called from within coroutine it should be fine.
So we want a separate function, e.g. get_current_event_loop(). Send a PR to the GitHub project.
Also see related bug: http://bugs.python.org/issue26969 <http://bugs.python.org/issue26969> Yuri did some work, but he argumented in favor of modifying get_event_loop. Should we continue discussion of implementation here or in that issue?

On Fri, May 20, 2016 at 10:53 AM, Ilya Kulakov <kulakov.ilya@gmail.com> wrote:
The tests are supposed to fail if something doesn't explicitly pass the loop; this is how we test that all code in asyncio itself always correctly passes the loop rather than accidentally relying on get_event_loop().
I consider it a feature that you can make get_event_loop() return None. But I think it would be a bug (in the Policy class) if it returned some other loop that's not the one that's running. I also think it's fine for a particular library or app to request that get_event_loop() not return None. User code is simpler that way. -- --Guido van Rossum (python.org/~guido)
participants (3)
-
Guido van Rossum
-
Ilya Kulakov
-
Vincent Michel