AnyIO Best Practices

Definitive guidelines for writing robust, maintainable, and backend-agnostic asynchronous Python code using AnyIO. Prioritize structured concurrency and native AnyIO primitives.

更新:2026-07-14

说明

# AnyIO Best Practices

AnyIO is the definitive choice for modern asynchronous Python development. It provides a unified, Trio-inspired API on top of `asyncio` or `Trio`, fixing critical design shortcomings of `asyncio` and enforcing structured concurrency. Adhere to these rules to build reliable, high-performance async applications.

## 1. Entry Point and Backend Selection

Always use `anyio.run()` as your application's entry point. Explicitly select your preferred backend and leverage backend-specific optimizations.

❌ **BAD**: Relying on default backend or `asyncio.run()`. ```python import asyncio from anyio import run, sleep

async def main(): print("Running on default backend (asyncio) without uvloop.") await sleep(0.1)

asyncio.run(main) # Bypasses AnyIO's entry point run(main) # Implicit backend, no optimizations ```

✅ **GOOD**: Explicit backend selection and performance enhancements. ```python from anyio import run, sleep import sniffio

async def main(): backend = sniffio.current_async_library() print(f"Hello from {backend}!") await sleep(0.1)

# For production, prefer

来源

https://github.com/sanjeed5/awesome-cursor-rules-mdc/blob/main/rules-mdc/anyio.mdc

← 返回全部 Skill