python-async-patterns
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePython Async Patterns
Python Asyncio 并发编程模式
Asyncio patterns for concurrent Python programming.
适用于Python并发编程的Asyncio模式。
Core Concepts
核心概念
python
import asynciopython
import asyncioCoroutine (must be awaited)
协程(必须使用await调用)
async def fetch(url: str) -> str:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.text()
async def fetch(url: str) -> str:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.text()
Entry point
入口函数
async def main():
result = await fetch("https://example.com")
return result
asyncio.run(main())
undefinedasync def main():
result = await fetch("https://example.com")
return result
asyncio.run(main())
undefinedPattern 1: Concurrent with gather
模式1:使用gather实现并发
python
async def fetch_all(urls: list[str]) -> list[str]:
"""Fetch multiple URLs concurrently."""
async with aiohttp.ClientSession() as session:
tasks = [fetch_one(session, url) for url in urls]
return await asyncio.gather(*tasks, return_exceptions=True)python
async def fetch_all(urls: list[str]) -> list[str]:
"""并发获取多个URL内容。"""
async with aiohttp.ClientSession() as session:
tasks = [fetch_one(session, url) for url in urls]
return await asyncio.gather(*tasks, return_exceptions=True)Pattern 2: Bounded Concurrency
模式2:有界并发
python
async def fetch_with_limit(urls: list[str], limit: int = 10):
"""Limit concurrent requests."""
semaphore = asyncio.Semaphore(limit)
async def bounded_fetch(url):
async with semaphore:
return await fetch_one(url)
return await asyncio.gather(*[bounded_fetch(url) for url in urls])python
async def fetch_with_limit(urls: list[str], limit: int = 10):
"""限制并发请求数量。"""
semaphore = asyncio.Semaphore(limit)
async def bounded_fetch(url):
async with semaphore:
return await fetch_one(url)
return await asyncio.gather(*[bounded_fetch(url) for url in urls])Pattern 3: TaskGroup (Python 3.11+)
模式3:TaskGroup(Python 3.11+)
python
async def process_items(items):
"""Structured concurrency with automatic cleanup."""
async with asyncio.TaskGroup() as tg:
for item in items:
tg.create_task(process_one(item))
# All tasks complete here, or exception raisedpython
async def process_items(items):
"""带有自动清理的结构化并发。"""
async with asyncio.TaskGroup() as tg:
for item in items:
tg.create_task(process_one(item))
# 所有任务在此处完成,或抛出异常Pattern 4: Timeout
模式4:超时处理
python
async def with_timeout():
try:
async with asyncio.timeout(5.0): # Python 3.11+
result = await slow_operation()
except asyncio.TimeoutError:
result = None
return resultpython
async def with_timeout():
try:
async with asyncio.timeout(5.0): # Python 3.11+
result = await slow_operation()
except asyncio.TimeoutError:
result = None
return resultCritical Warnings
重要注意事项
python
undefinedpython
undefinedWRONG - blocks event loop
错误示例 - 阻塞事件循环
async def bad():
time.sleep(5) # Never use time.sleep!
requests.get(url) # Blocking I/O!
async def bad():
time.sleep(5) # 绝不要使用time.sleep!
requests.get(url) # 阻塞式I/O操作!
CORRECT
正确示例
async def good():
await asyncio.sleep(5)
async with aiohttp.ClientSession() as s:
await s.get(url)
```pythonasync def good():
await asyncio.sleep(5)
async with aiohttp.ClientSession() as s:
await s.get(url)
```pythonWRONG - orphaned task
错误示例 - 孤立任务
async def bad():
asyncio.create_task(work()) # May be garbage collected!
async def bad():
asyncio.create_task(work()) # 可能会被垃圾回收!
CORRECT - keep reference
正确示例 - 保留引用
async def good():
task = asyncio.create_task(work())
await task
undefinedasync def good():
task = asyncio.create_task(work())
await task
undefinedQuick Reference
速查表
| Pattern | Use Case |
|---|---|
| Multiple independent operations |
| Rate limiting, resource constraints |
| Structured concurrency (3.11+) |
| Producer-consumer |
| Timeout wrapper (3.11+) |
| Shared mutable state |
| 模式 | 使用场景 |
|---|---|
| 多个独立操作 |
| 速率限制、资源约束 |
| 结构化并发(3.11+) |
| 生产者-消费者模型 |
| 超时包装器(3.11+) |
| 共享可变状态控制 |
Async Context Manager
异步上下文管理器
python
from contextlib import asynccontextmanager
@asynccontextmanager
async def managed_connection():
conn = await create_connection()
try:
yield conn
finally:
await conn.close()python
from contextlib import asynccontextmanager
@asynccontextmanager
async def managed_connection():
conn = await create_connection()
try:
yield conn
finally:
await conn.close()Additional Resources
额外资源
For detailed patterns, load:
- - Queue, Lock, producer-consumer
./references/concurrency-patterns.md - - HTTP client/server patterns
./references/aiohttp-patterns.md - - run_in_executor, thread pools
./references/mixing-sync-async.md - - Debug mode, profiling, finding issues
./references/debugging-async.md - - Graceful shutdown, health checks, signal handling
./references/production-patterns.md - - Retry with backoff, circuit breakers, partial failures
./references/error-handling.md - - uvloop, connection pooling, buffer sizing
./references/performance.md
如需详细模式说明,请查看:
- - 队列、锁、生产者-消费者模型
./references/concurrency-patterns.md - - HTTP客户端/服务器模式
./references/aiohttp-patterns.md - - run_in_executor、线程池
./references/mixing-sync-async.md - - 调试模式、性能分析、问题排查
./references/debugging-async.md - - 优雅关闭、健康检查、信号处理
./references/production-patterns.md - - 指数退避重试、断路器、部分故障处理
./references/error-handling.md - - uvloop、连接池、缓冲区大小配置
./references/performance.md
Scripts
脚本工具
- - Scan code for blocking calls in async functions
./scripts/find-blocking-calls.sh
- - 扫描异步函数中的阻塞调用
./scripts/find-blocking-calls.sh
Assets
资源文件
- - Production-ready async app skeleton
./assets/async-project-template.py
- - 生产级异步应用骨架
./assets/async-project-template.py
See Also
另请参阅
Prerequisites:
- - Type hints for async functions
python-typing-patterns
Related Skills:
- - Async web APIs
python-fastapi-patterns - - Async logging and tracing
python-observability-patterns - - Async database access
python-database-patterns
前置要求:
- - 异步函数的类型提示
python-typing-patterns
相关技能:
- - 异步Web API
python-fastapi-patterns - - 异步日志与追踪
python-observability-patterns - - 异步数据库访问
python-database-patterns