anyio — Cursor rule: what it does, how to install
This rule guides asynchronous Python work with AnyIO, including explicit backend selection and structured concurrency. It targets *.py files.
.cursor/rules/anyio.mdcWhat anyio does
The rule covers asynchronous Python code using AnyIO over asyncio or Trio. It recommends starting an application with anyio.run() rather than asyncio.run(), and choosing the backend explicitly instead of relying on the default. Its example uses sniffio to identify the active async library.
Place the rule at .cursor/rules/anyio.mdc. Its **/*.py glob means other file types are outside its scope. If your application currently starts with asyncio.run(), the rule points to a concrete change: use anyio.run() as the entry point.
What it helps with
- replace
asyncio.run()withanyio.run() - select an AnyIO backend explicitly
- identify the active async library with
sniffio - apply async Python guidance to matching files
Example prompts for the agent
- Update this async Python entry point to use
anyio.run()and select a backend explicitly. - Review this Python code against the rule's AnyIO structured-concurrency guidance.
- Use
sniffioto identify the active async library in this entry point.
How to install anyio
Manually: put the files in place
.cursor/rules/anyio.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.claude/rules/anyio.mdThe paths frontmatter field limits the files: move globs from the source into paths
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.github/instructions/anyio.instructions.mdThe applyTo field sets which files it applies to: move globs from the source into applyTo
Manually: put the files in place
.devin/rules/anyio.mdManually: put the files in place
.clinerules/anyio.mdStep by step, with commands for each platform: Cursor rules: .mdc files, four modes and what replaced .cursorrules
Questions about anyio
How do I install anyio in Cursor?
Place the rule file at .cursor/rules/anyio.mdc.
What does the anyio rule do?
It guides asynchronous Python code using AnyIO, including the entry point and explicit backend selection.
Which files does the anyio rule cover?
Its **/*.py glob targets Python files.
Source text
anyio.mdc · CC0-1.0
---
description: Definitive guidelines for writing robust, maintainable, and backend-agnostic asynchronous Python code using AnyIO. Prioritize structured concurrency and native AnyIO primitives.
globs: **/*.py
---
# 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
© sanjeed5 · CC0-1.0 · sanjeed5/awesome-cursor-rules-mdc