I have just started learning asyncio in Python, and wrote the following code:
import asyncio
loop = asyncio.get_event_loop()
async def hello():
print("Hello")
await asyncio.sleep(3)
print("World")
if __name__ == '__main__':
for i in range(3):
loop.run_until_complete(hello())
loop.close()
This gives me the following output:
Hello
<waits for 3 seconds>
World
Hello
<waits for 3 seconds>
World
Hello
<waits for 3 seconds>
World
How can I modify the program so that the same is handled asynchronously (i.e. once the first task sleeps, the second one begins executing), essentially giving output similar to this:
Hello
Hello
Hello
World
World
World