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 asyncioimport nest_asyncioasyncdef async_task(item):try:await asyncio.sleep(1) # Simulate async workprint(f"Task completed for {item}")exceptExceptionas e:print(f"Task failed for {item}: {e}")asyncdef main(items): tasks = [asyncio.create_task(async_task(item)) for item in items]await asyncio.gather(*tasks)# Example usageitems = ["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.
Source Code
---title: "Running Async Tasks in a Loop"author: "Andres Monge <aemonge>"date: "2024-12-17"format: html: smooth-scroll: true code-fold: true code-tools: true code-copy: true code-annotations: true---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 ExecutionUse `asyncio.create_task` to schedule tasks concurrently and `asyncio.gather`to wait for all tasks to complete.#### Example: Running Tasks Concurrently```{python}import asyncioimport nest_asyncioasyncdef async_task(item):try:await asyncio.sleep(1) # Simulate async workprint(f"Task completed for {item}")exceptExceptionas e:print(f"Task failed for {item}: {e}")asyncdef main(items): tasks = [asyncio.create_task(async_task(item)) for item in items]await asyncio.gather(*tasks)# Example usageitems = ["item1", "item2", "item3"]if__name__=="__main__": nest_asyncio.apply() asyncio.run(main(items))```### 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.