惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
MyScale Blog
MyScale Blog
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
The Blog of Author Tim Ferriss
Martin Fowler
Martin Fowler
博客园 - 【当耐特】
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
宝玉的分享
宝玉的分享
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
C
Check Point Blog
Last Week in AI
Last Week in AI
F
Fortinet All Blogs
博客园 - 聂微东
Blog — PlanetScale
Blog — PlanetScale
H
Help Net Security
GbyAI
GbyAI
云风的 BLOG
云风的 BLOG

MachineLearningMastery.com

A Gentle Introduction to Model Distillation - MachineLearningMastery.com Fine-Tuning Agentic AI: A Practical Guide - MachineLearningMastery.com How to Combine Traditional Machine Learning with Agentic Reasoning - MachineLearningMastery.com Versioning and Tracking Scikit-LLM Experiments - MachineLearningMastery.com Chain of Thought vs. Tree of Thoughts: Which is Best for AI Agents? - MachineLearningMastery.com Dataclasses for Structured Application Data - MachineLearningMastery.com Single-Agent vs. Multi-Agent Systems: When the Complexity Is Worth It - MachineLearningMastery.com AI Agent Memory Design: What Works and What Doesn’t 3 Ways to Enhance Your AI Model's Interpretability - MachineLearningMastery.com Combining LLM Embeddings with Tabular Features in a Unified Scikit-learn Pipeline - MachineLearningMastery.com Interpretable Text Classification: Probing Scikit-LLM Embedding Spaces - MachineLearningMastery.com Learn Vectorized Thinking in Python Through Examples - MachineLearningMastery.com Comparing Local Tool Calling: Gemma 4 vs. Llama 3 vs. Mistral - MachineLearningMastery.com Integrating Agentic AI with Existing Machine Learning Pipelines - MachineLearningMastery.com How to Build a Robust RAG System with Minimal Resources - MachineLearningMastery.com Managing Small Context Windows in Language Models - MachineLearningMastery.com 7 Regression Tests Every AI Agent Should Pass Before Deploy - MachineLearningMastery.com Understanding the Role of Latent Space in Machine Learning Models - MachineLearningMastery.com Retrieval vs. Memory in Agentic AI System Prompt Caching vs. Fine-Tuning: A Cost and Latency Decision Framework - MachineLearningMastery.com Identifying Token Costs Hiding in Your Agentic Loop - MachineLearningMastery.com Designing AI Agents That Can Self-Correct - MachineLearningMastery.com 7 Chunking Strategies That Decide Whether Your RAG Works - MachineLearningMastery.com Measuring Performance of Transformer Inference - MachineLearningMastery.com Static vs. Dynamic vs. Continuous Batching in LLM Inference Decoding Strategies and Output Control - MachineLearningMastery.com Using a Transformer Model: From Training to Inference The End-to-End Agentic AI Pipeline Ollama vs. LM Studio vs. llama.cpp: Which Local AI Runtime Should You Use in 2026? 5 Architectural Patterns for Persistent Memory and State in AI Agents - MachineLearningMastery.com
7 Async Patterns for Running Agents Concurrently in Pytho...
Vinod Chugani · 2026-08-11 · via MachineLearningMastery.com

In this article, you will learn seven async patterns for running AI agents concurrently in Python, what each pattern is suited for, and the production-level pitfalls to watch out for with each.

Topics we will cover include:

  • Core async patterns such as fire and forget, scatter-gather, task groups, and producer-consumer queues, and when to reach for each one.
  • Resource-management techniques including semaphore-based backpressure and speculative execution, along with their real-world trade-offs.
  • How to chain agents into asynchronous pipelines and keep your event loop healthy under load.

7 Async Patterns Running Agents Concurrently Python

Orchestrating a single AI agent is simple enough. Keeping a fleet of them running concurrently without deadlocking your event loop or triggering cascading rate limit errors is a different problem entirely.

Python’s asyncio library gives you the primitives to manage this. But the patterns you reach for matter. Each one solves a different coordination problem, and picking the wrong one creates failure modes that are slow to surface and hard to debug.

Here are seven async patterns for running agents concurrently, along with the production catches that come with each.

1. Fire and Forget (Detached Background Execution)

You launch an agent task and move on without waiting for it to finish. The coroutine runs in the background while your main execution path continues.

This works well when the task outcome doesn’t affect anything downstream: logging, flushing context to storage, or triggering a background cleanup agent.

Watch out for: Exceptions in detached tasks are silently swallowed by the event loop. If a background agent fails, nothing alerts you unless you explicitly attach an error callback. Wire in exception handling before treating any task as truly safe to ignore.

2. Strict Scatter-Gather

You fan out from one orchestrator agent to multiple worker agents simultaneously, then wait for all of them to return before continuing.

asyncio.gather() multiplexes outbound requests and assembles results in launch order. Think five agents querying different data sources in parallel, with results collected once the last one finishes.

Watch out for: By default, a single failure cancels the rest. Even when you disable that behavior, straggler latency still applies — the whole operation waits on the slowest agent. One slow generation bottlenecks everything else.

3. Supervised Task Groups

Introduced in Python 3.11, task groups give you a structured version of gather. A context manager makes the scope of concurrent tasks explicit: when the block exits, all tasks are either complete or cancelled, and errors surface immediately.

For new projects on Python 3.11+, task groups are generally the cleaner choice over managing a loose collection of tasks manually.

Watch out for: Task groups aggressively cancel sibling tasks on failure. If one worker hits a rate limit error, every other running agent gets cancelled. Build retry logic inside individual agent coroutines before letting exceptions reach the group level.

4. Producer-Consumer with Queues

Not all agents start at the same time. Sometimes one agent generates work and others process it, and a queue sits between them as a buffer.

Producer agents add items to the queue as they find work. Consumer agents pull from it independently. The two sides don’t need to know anything about each other, and you can scale consumers up or down without touching the producer.

Watch out for: Unbounded queues leak memory silently. If your producer generates tasks faster than consumers can process them, the queue grows until your process runs out of RAM. Set a maximum queue size to enforce backpressure on the producer.

5. Backpressure via Semaphores

You set a hard limit on how many agents can access a resource at the same time. Agents that exceed the limit wait their turn rather than all firing simultaneously.

This is one of the most practical patterns for production agent systems, where external APIs, database connection pools, and internal services all have throughput ceilings.

Watch out for: Semaphores limit connections, not tokens. You can cap concurrent requests at 10 and still blow through a provider’s tokens-per-minute limit if all 10 agents are generating large outputs at once. For strict API compliance, pair semaphores with token-aware throttling.

6. Speculative Execution (First Completed Wins)

You race multiple agents against the same goal and cancel the losers the moment one returns a valid result. This trades compute efficiency for speed.

A common use case is racing a smaller, faster model against a larger, slower one and accepting whichever finishes within your latency target.

Watch out for: Cancelling a task drops your local connection but doesn’t stop generation on the provider’s servers. The model keeps running and consuming tokens on your account even after you’ve moved on. You pay for every losing agent, every time.

7. Asynchronous Pipeline Chaining

Each agent in a chain takes the output of the previous one as input. Agent A fetches raw data, Agent B cleans it, Agent C analyzes it, Agent D formats the output.

This maps well to multi-stage retrieval pipelines and reasoning workflows where each stage has a distinct responsibility, isolated error handling, and potentially different model settings.

Watch out for: Tracing failures back through the chain is hard without instrumentation. By the time Agent D crashes on a malformed input, the schema violation may have started in Agent A. Inject tracing identifiers into the payloads passed between stages.

Discussion

Here are some quick hits on choosing the right pattern:

  • Independent tasks, all needed: scatter-gather or task groups
  • Streaming or unknown-volume workloads: producer-consumer with a queue
  • External resources with rate limits: backpressure via semaphores
  • Speed over completeness: speculative execution
  • Sequential logic across specialized agents: pipeline chaining
  • Background tasks with no return value needed: fire and forget

Most production systems combine two or three of these. A pipeline might use semaphores inside each stage. A producer-consumer setup might use gather within each consumer pool.

One more thing: watching your event loop

Even with perfectly async networking, synchronous CPU-bound operations — such as heavy JSON parsing or running a tokenizer — will block the event loop. When the loop blocks, in-flight requests miss their timeout heartbeats and trigger cascading failures across your otherwise async architecture.

Profile your loop regularly and offload CPU-heavy operations to a thread pool when they show up as bottlenecks. The patterns above handle I/O-bound coordination. Keeping the loop clear is what makes them hold up.

Conclusion

These seven patterns give you a vocabulary for thinking about agent coordination before problems surface in production. Start with gather or task groups for simple cases, layer in semaphores and queues as complexity grows, and treat the “watch out for” notes as the parts most likely to cost you at scale.

The patterns are the architecture. Getting them right is what separates a fragile prototype from a system that stays up.

No comments yet.