python-async

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Async 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
    modules/basic-patterns.md
    - Core async/await, gather(), and task management
  • See
    modules/concurrency-control.md
    - Semaphores and locks for rate limiting
  • See
    modules/error-handling-timeouts.md
    - Error handling, timeouts, and cancellation
  • See
    modules/advanced-patterns.md
    - Context managers, iterators, producer-consumer
  • See
    modules/testing-async.md
    - Testing with pytest-asyncio
  • See
    modules/real-world-applications.md
    - Web scraping and database operations
  • See
    modules/pitfalls-best-practices.md
    - Common mistakes and best practices
Load specific modules based on your needs, or reference all for detailed guidance.
本技能采用渐进式加载,内容划分为多个聚焦的模块:
  • 查看
    modules/basic-patterns.md
    ——核心async/await、gather()及任务管理
  • 查看
    modules/concurrency-control.md
    ——用于限流的信号量与锁
  • 查看
    modules/error-handling-timeouts.md
    ——错误处理、超时与取消
  • 查看
    modules/advanced-patterns.md
    ——上下文管理器、迭代器、生产者-消费者模式
  • 查看
    modules/testing-async.md
    ——使用pytest-asyncio进行测试
  • 查看
    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
asyncio.run()
as the entry point. Avoid
get_event_loop()
in Python 3.10+.
Blocking call in async context Move sync I/O to
asyncio.to_thread()
or
loop.run_in_executor()
.
Tests hang indefinitely Ensure pytest-asyncio is installed and test functions are decorated with
@pytest.mark.asyncio
.
RuntimeError: no current event loop 请使用
asyncio.run()
作为入口点。Python 3.10+版本中避免使用
get_event_loop()
异步上下文中存在阻塞调用 将同步I/O移至
asyncio.to_thread()
loop.run_in_executor()
中执行。
测试无限挂起 请确保已安装pytest-asyncio,且测试函数添加了
@pytest.mark.asyncio
装饰器。