Why is there a different how tasks are run between tasks 1-3 and 4-6 in the code below?
Code:
import asyncio
async def do_something(i, sleep): # No I/O here
print("Doing... ", end="")
print(await no_io_1(i, sleep))
async def no_io_1(i, sleep): # No I/O here
return await no_io_2(i, sleep)
async def no_io_2(i, sleep): # No I/O here
return await io(i, sleep)
async def io(i, sleep):
await asyncio.sleep(sleep) # Finally some I/O
# t = asyncio.create_task(asyncio.sleep(sleep))
# await t
return i
async def main():
await asyncio.create_task(do_something(1, sleep=4))
await asyncio.create_task(do_something(2, sleep=3))
await asyncio.create_task(do_something(3, sleep=2))
t4 = asyncio.create_task(do_something(4, sleep=4))
t5 = asyncio.create_task(do_something(5, sleep=3))
t6 = asyncio.create_task(do_something(6, sleep=2))
await t4
await t5
await t6
asyncio.run(main())
print("\r\nBye!")
Output:
Doing... 1
Doing... 2
Doing... 3
Doing... Doing... Doing... 6
5
4