I am having difficulties understanding the output of the following program
What I think is happening:
We create a task which is the
The first time I ran this I expected the output to be
import asyncio async def inner(): try: print("Inside inner") await asyncio.sleep(3) print("Awaken inner") except asyncio.CancelledError: print("Caught cancel exception in spawner") async def main(): try: a = asyncio.create_task(asyncio.wait_for(inner(), timeout=2.0)) await a except asyncio.TimeoutError: print("Timeout") await asyncio.sleep(5) asyncio.run(main())The output is
Output:Inside inner
Caught cancel exception in inner
I undestand that this might be because I am not familiar with the inner details of asyncio, as I don't really understand what are Futures and such.What I think is happening:
We create a task which is the
wait_for
coroutine wrapping the inner
coroutine. From my little understanding I expected that since inner
will timeout, wait_for
would raise a TimeoutError
, however, since wait_for
is wrapped in create_task
this will no propagate up towards the line await a
. Is this assumption correct?The first time I ran this I expected the output to be
Output:Inside inner
Timeout
Caught cancel exception in inner
Thank you, I would highly appreciate a more in-depth explanation of what actually happens!