← ClaudeAtlas

async-python-patternslisted

Async Python for high-performance services. Use when implementing FastAPI/Temporal/Kafka async handlers, AsyncSession database access, asyncio.gather concurrency, or diagnosing event loop blocking.
ajyadav013/claude-kit · ★ 12 · AI & Automation · score 72
Install: claude install-skill ajyadav013/claude-kit
Async programming patterns for high-performance Python services. ## When to use - Implementing async FastAPI handlers, Temporal activities, or Kafka message processors - Setting up database access with AsyncSession (SQLAlchemy 2.0 + asyncpg) - Running multiple workers, consumers, or health tasks concurrently with asyncio.gather - Bridging sync and async code (blocking Kafka consumer dispatching async handlers, or calling sync cloud clients from async activities) - Diagnosing event loop blocking or performance issues (the sync-in-async trap) - Implementing graceful shutdown for workers with SIGTERM/SIGINT handlers - Choosing or implementing an async HTTP client wrapper ## Core conventions 1. **Fully async stack:** Use `async def` handlers, `AsyncSession` over asyncpg, aiokafka for producers/consumers (when SASL_SSL+GSSAPI Kerberos is not required), aiohttp for HTTP clients. For Kerberos auth, use kafka-python with the sync→async bridge pattern. 2. **Lifespan for FastAPI apps:** Use `@asynccontextmanager async def lifespan(app)` to initialize connections on startup and close them on shutdown; pass `lifespan=lifespan` to `FastAPI()`. 3. **AsyncSession setup:** Create async engine with `create_async_engine(url.replace("postgresql://", "postgresql+asyncpg://"), pool_pre_ping=True, ...)`, use `async_scoped_session(sessionmaker(..., expire_on_commit=False, class_=AsyncSession), scopefunc=current_task)` for task-local sessions. The `current_task` scope ensures each asyncio task g