I watched an asyncio Pycon video on youtube where the presenter in one of her examples said she was using asyncio.create_task
to run the coroutine instead of await
, as await
would block the execution of the coroutine until whatever it is awaiting is complete. I thought asyncio.create_task
returns a Task
which needs to be await. Nevertheless, I ran test using the following code and I am somewhat surprised by its result.
async def say_hello():
print("Hello")
await asyncio.sleep(0.5)
print("World")
async def run_coro(coro):
asyncio.create_task(coro)
asyncio.run(run_coro(say_hello()))
The code above prints only Hello
. I figured that asyncio.create_task
in run_coro
stops execution as soon as it reaches the await
line in say_hello
. If I rewrite say_hello
as follows and the run it using run_coro
I see all the lines before `await asyncio.sleep(0.5) printed on the terminal
async def say_hello():
print("Hello")
print("Hello")
print("Hello")
print("Hello")
await asyncio.sleep(0.5)
print("World")
If I rewrite run_coro
as follows then all the lines get printed as expected:
async def run_coro(coro):
asyncio.create_task(coro)
await asyncio.sleep(1)
Can anyone tell why?