The is the simple test code and the result.
import asyncio
async def test():
await asyncio.sleep(1)
if __name__ == \'__main__\':
asyncio.set_event
I want to know it is normal for
asyncio
that main loop Must be set and is used for coroutines regardless of a loop over which coroutine is run.
This used to be required prior to Python 3.6. The reason is that functions like asyncio.sleep()
need an event loop to be able to use loop.call_later()
to schedule a wake-up call to complete the future.
As of Python 3.6 (or 3.5.3, which included a fix for the issue), when get_event_loop()
is invoked from a coroutine driven by an event loop, it always returns the event loop that drives it. As a result, your code works correctly.
The new behavior is not mentioned in the online documentation, but is in the docstring:
When called from a coroutine or a callback (e.g. scheduled with
call_soon
or similar API), this function will always return the running event loop.