python-async
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAsync Python Patterns
Python异步模式
asyncio and async/await patterns for Python applications.
适用于Python应用的asyncio与async/await模式。
Quick Start
快速开始
python
import asyncio
async def main():
print("Hello")
await asyncio.sleep(1)
print("World")
asyncio.run(main())python
import asyncio
async def main():
print("Hello")
await asyncio.sleep(1)
print("World")
asyncio.run(main())When To Use
适用场景
- Building async web APIs (FastAPI, aiohttp)
- Implementing concurrent I/O operations
- Creating web scrapers with concurrent requests
- Developing real-time applications (WebSockets)
- Processing multiple independent tasks simultaneously
- Building microservices with async communication
- 构建异步Web API(FastAPI、aiohttp)
- 实现并发I/O操作
- 开发支持并发请求的网络爬虫
- 开发实时应用(WebSockets)
- 同时处理多个独立任务
- 构建支持异步通信的微服务
When NOT To Use
不适用场景
- CPU-bound optimization - use python-performance instead
- Testing async code - use python-testing async module
- CPU密集型优化——请改用python-performance
- 异步代码测试——请使用python-testing异步模块
Modules
模块
This skill uses progressive loading. Content is organized into focused modules:
- See - Core async/await, gather(), and task management
modules/basic-patterns.md - See - Semaphores and locks for rate limiting
modules/concurrency-control.md - See - Error handling, timeouts, and cancellation
modules/error-handling-timeouts.md - See - Context managers, iterators, producer-consumer
modules/advanced-patterns.md - See - Testing with pytest-asyncio
modules/testing-async.md - See - Web scraping and database operations
modules/real-world-applications.md - See - Common mistakes and best practices
modules/pitfalls-best-practices.md
Load specific modules based on your needs, or reference all for detailed guidance.
本技能采用渐进式加载,内容划分为多个聚焦的模块:
- 查看 ——核心async/await、gather()及任务管理
modules/basic-patterns.md - 查看 ——用于限流的信号量与锁
modules/concurrency-control.md - 查看 ——错误处理、超时与取消
modules/error-handling-timeouts.md - 查看 ——上下文管理器、迭代器、生产者-消费者模式
modules/advanced-patterns.md - 查看 ——使用pytest-asyncio进行测试
modules/testing-async.md - 查看 ——网络爬虫与数据库操作
modules/real-world-applications.md - 查看 ——常见错误与最佳实践
modules/pitfalls-best-practices.md
可根据你的需求加载特定模块,或参考所有模块获取详细指引。
Exit Criteria
退出标准
- Async patterns applied correctly
- No blocking operations in async code
- Proper error handling implemented
- Rate limiting configured where needed
- Tests pass with pytest-asyncio
- 正确应用异步模式
- 异步代码中无阻塞操作
- 实现了合适的错误处理
- 按需配置了限流规则
- 基于pytest-asyncio的测试全部通过
Troubleshooting
故障排除
Common Issues
常见问题
RuntimeError: no current event loop
Use as the entry point. Avoid in Python 3.10+.
asyncio.run()get_event_loop()Blocking call in async context
Move sync I/O to or .
asyncio.to_thread()loop.run_in_executor()Tests hang indefinitely
Ensure pytest-asyncio is installed and test functions are decorated with .
@pytest.mark.asyncioRuntimeError: no current event loop
请使用作为入口点。Python 3.10+版本中避免使用。
asyncio.run()get_event_loop()异步上下文中存在阻塞调用
将同步I/O移至或中执行。
asyncio.to_thread()loop.run_in_executor()测试无限挂起
请确保已安装pytest-asyncio,且测试函数添加了装饰器。
@pytest.mark.asyncio