Python Async Runtime
Priority: P1 (HIGH)
Rules
- Keep blocking DB, subprocess, and legacy HTTP calls out of event-loop code.
- Use
asyncio.to_threadonly as an explicit boundary around blocking libraries. - Make timeout and cancellation behavior visible in the call path.
- Track long-lived tasks, cleanup, and retries deliberately.
Recipe
- Use async-native clients first.
- Wrap blocking calls once at the adapter seam.
- Propagate cancellation and timeout context instead of swallowing it.
- Separate poll loop logic from work-item processing.
- Test the timeout and error path, not only the happy path.
Anti-Patterns
- No direct blocking I/O inside
async def. - No hidden infinite loops without stop condition or observability.
- No broad
except Exceptionaround await chains that erases task state. - No orphan background tasks without lifecycle ownership.
References
- Framework Map
- Async Boundaries