ajyadav013/claude-kit/async-python-patterns
Claude Code and agent skill by ajyadav013/claude-kit
Trust tier 3
- community
Source check pending
Checking that the SKILL.md file is still in the repository.
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.
Install
npx vskill@latest install ajyadav013/claude-kit/async-python-patternsSKILL.md
The same SKILL.md was published earlier as ajyadav013/claude-kit/async-python-patterns.
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
- Fully async stack: Use
async defhandlers,AsyncSessionover 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. - Lifespan for FastAPI apps: Use
@asynccontextmanager async def lifespan(app)to initialize connections on startup and close them on shutdown; passlifespan=lifespantoFastAPI(). - AsyncSession setup: Create async engine with
create_async_engine(url.replace("postgresql://", "postgresql+asyncpg://"), pool_pre_ping=True, ...), useasync_scoped_session(sessionmaker(..., expire_on_commit=False, class_=AsyncSession), scopefunc=current_task)for task-local sessions. Thecurrent_taskscope ensures each asyncio task gets its own session. - asyncio.gather for concurrency: Run multiple Temporal workers, Kafka consumers, or health tasks together with
await asyncio.gather(*tasks, return_exceptions=True). No TaskGroup observed. - Sync-in-async BRIDGE pattern (Kafka consumer): When a blocking library (kafka-python) must dispatch async handlers, run the sync consumer in a thread via
asyncio.to_thread(_run_kafka_consumer, loop, state)and dispatch coroutines back to the event loop withasyncio.run_coroutine_threadsafe(coro, loop).result(). - AVOID the sync-in-async TRAP: NEVER call sync cloud clients (google-cloud-bigquery, google-cloud-storage) directly from async code without wrapping in
asyncio.to_thread(...)orloop.run_in_executor(None, ...). They block the event loop. Use async clients (gcloud-aio-storage) or offload to a thread. - Graceful worker shutdown: Use
loop.add_signal_handler(signal.SIGTERM, on_signal_received)to triggerawait worker.shutdown()thenawait asyncio.gather(*worker_tasks, return_exceptions=True). - Async HTTP clients: Prefer lightweight wrappers over aiohttp (e.g., AsyncHTTPClient pattern) or an async request library with unified protocol interface, circuit breaker, retry, and pydantic models.
- uvloop for performance (optional): Some services add uvloop as a dependency; it's not universal but improves event loop throughput.
Skeleton / example
Anti-patterns to avoid
- Calling sync cloud clients from async code without offloading:
bigquery.Client().query(...)orstorage.Client().get_bucket(...)directly in an async function blocks the event loop. Wrap inasyncio.to_thread(...)or use async clients. - Creating a new event loop per handler in a threaded context: Use
asyncio.run_coroutine_threadsafe(coro, loop)to dispatch onto the existing loop, notasyncio.run(coro)which spawns a second loop. - Ignoring signal handlers in workers: Workers without SIGTERM/SIGINT handlers will be killed mid-flight. Always call
worker.shutdown()and await pending tasks withreturn_exceptions=True. - Not using `return_exceptions=True` in gather: Omitting this causes gather to raise on the first exception, leaving other tasks dangling. Always use it for concurrent worker/health tasks.
- Mixing sync and async session factories: Don't use
sessionmaker(..., class_=Session)in async code. Alwaysclass_=AsyncSessionandcreate_async_enginewith thepostgresql+asyncpg://driver. - Using kafka-python when aiokafka is available: The blocking kafka-python consumer requires the complex sync→async bridge pattern. Prefer aiokafka (fully async) unless you need GSSAPI/Kerberos auth (not yet supported by aiokafka as of 0.7.x).
References
Security
- Security scanPassed, 90 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/async-python-patterns/SKILL.md
Popularity
- 7-day trend score
- 5
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/ajyadav013/claude-kit/async-python-patterns)