Event loop created by asyncio.new_event_loop freezes

The following code just hangs without even printing anything:

import asyncio async def foo(loop): print('foo') loop.stop() loop = asyncio.new_event_loop() asyncio.ensure_future(foo(loop)) loop.run_forever() 

If I use get_event_loop everything works fine. Is there something I am doing wrong, or am I stumbled upon a mistake?

I am using Python 3.5.1.

+3
source share
1 answer

asyncio.AbstractEventLoopPolicy.new_event_loop documentation says:

If theres need to set this loop as an event loop for the current context, set_event_loop() must be explicitly specified.


 import asyncio async def foo(loop): print('foo') loop.stop() loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) # <---- asyncio.ensure_future(foo(loop)) loop.run_forever() 
+6
source

Source: https://habr.com/ru/post/1244642/


All Articles