Sobes.tech
Junior — Senior

How to convert sequential coroutines into parallel ones?

livecode

Task condition

What needs to be changed in this code snippet for the coroutines to run concurrently instead of sequentially? Example code with asynchronous functions called one after another.

import asyncio
async def coro(sleep_time):
    print(f"Sleeping for {sleep_time} seconds")
    await asyncio.sleep(sleep_time)
    print(f"Done sleeping for {sleep_time} seconds")

async def main():
    task1 = coro(1)
    task2 = coro(2)
    task3 = coro(3)
    await task1
    await task2
    await task3

asyncio.run(main())
How to convert sequential coroutines into parallel… - sobes.tech