Asyncio tasks getting cancelled

ike at koeln.ccc.de ike at koeln.ccc.de
Sun Nov 4 14:25:31 EST 2018


Hi all,

I'm having trouble with asyncio. Apparently tasks (asyncio.create_task)
are not kept referenced by asyncio itself, causing the task to be
cancelled when the creating function finishes (and noone is awaiting the
corresponding futue). Am I doing something wrong or is this expected
behavior?

The code sample I tried:

> import asyncio
> 
> async def foobar():
>     print(1)
>     await asyncio.sleep(1)
>     print(2)
> 
> async def main():
>     asyncio.create_task(foobar())
>     #await asyncio.sleep(2)
> 
> loop = asyncio.get_event_loop()
> asyncio.run(main())
> loop.run_forever()

The sample does print "2" only when uncommenting the asyncio.sleep(2)
line.

Greetings.



More information about the Python-list mailing list