Running Async Tasks in a Loop

Author

Andres Monge

Published

December 17, 2024

When working with asynchronous tasks in Python, you may want to run multiple tasks concurrently without blocking the loop. Here’s how to do it using asyncio.create_task and asyncio.gather.

Concurrent Task Execution

Use asyncio.create_task to schedule tasks concurrently and asyncio.gather to wait for all tasks to complete.

Example: Running Tasks Concurrently

Code
import asyncio
import nest_asyncio

async def async_task(item):
    try:
        await asyncio.sleep(1)  # Simulate async work
        print(f"Task completed for {item}")
    except Exception as e:
        print(f"Task failed for {item}: {e}")

async def main(items):
    tasks = [asyncio.create_task(async_task(item)) for item in items]
    await asyncio.gather(*tasks)

# Example usage
items = ["item1", "item2", "item3"]

if __name__ == "__main__":
    nest_asyncio.apply()
    asyncio.run(main(items))
Task completed for item1
Task completed for item2
Task completed for item3

Key Points

  • asyncio.create_task: Schedules a coroutine to run concurrently.
  • asyncio.gather: Waits for all tasks to complete.
  • Error Handling: Use try-except to handle task failures gracefully.

This approach ensures tasks run concurrently without blocking the loop.