async-python-patterns
Master Python asyncio, concurrent programming, and async/await patterns for high-performance applications. Use when building async APIs, concurrent systems, or I/O-bound applications requiring non-blocking operations.
- License 许可
- MIT
- 最近更新
- 2026-11-12
- Stars
- ★ 1
- Forks
- 0
01 前言 Front Matter
- name
- async-python-patterns
- metadata
- { "skill-graph-id": "plugin/10x-swe/async-python-patterns", "skill-graph-uses": "", "skill-graph-used-by": "" }
- description
- Master Python asyncio, concurrent programming, and async/await patterns for high-performance applications. Use when building async APIs, concurrent systems, or I/O-bound applications requiring non-blocking operations.
02 资源文件 1
- 参考文档references/patterns.md20.2 KB
03 SKILL.md 正文
Async Python Patterns
Use asyncio for concurrent I/O when a synchronous implementation cannot meet the requirement. Inspect the supported Python version and existing async framework before selecting APIs.
Workflow
- Identify the workload: concurrent network or database I/O, blocking library calls, or CPU work. Keep simple scripts synchronous. Threads can offload blocking I/O; pure Python CPU parallelism generally requires processes unless the workload releases the GIL.
- Mark sync/async boundaries explicitly. Await coroutines and use async-native clients. Offload necessary blocking calls; never call
asyncio.run()inside an already running event loop. - Define task ownership, maximum concurrency, queue capacity, deadlines, and shutdown behavior. A semaphore limits concurrent operations; it does not enforce requests per second.
- Reuse connection pools and close responses, sessions, and database resources. Use async context managers and failure-safe cleanup.
- Choose the failure policy before launching tasks: fail the group, collect individual failures, or retry bounded transient errors. Inspect every collected exception; do not silently discard failed items.
- On cancellation, clean up and propagate
CancelledError. Cancel and await owned tasks during shutdown. Ensure each queue item receivestask_done(), including sentinels, and provide a shutdown path for every consumer. - Test successful completion, timeout, cancellation, partial failure, resource cleanup, and concurrency limits. Measure latency and resource usage under representative load.
Reference routing
Use detailed patterns only for the needed topic:
- Fundamental Patterns: coroutines,
gather, task creation, errors, and timeouts. - Advanced Patterns: context managers, iterators, queues, semaphores, and locks.
- Real-World Applications: HTTP, database, and WebSocket sketches.
- Performance Best Practices: pooling, batches, and blocking-call boundaries.
- Common Pitfalls and Testing Async Code: cancellation and pytest examples.
These preserved examples contain simplifications. The producer-consumer sketch has fewer sentinels than consumers and does not await cancelled tasks; the HTTP pooling sketch leaves responses unconsumed; database query strings are illustrative and must use driver parameters in real code. An asyncio lock synchronizes tasks on one loop, not arbitrary threads. Adapt rather than copy.
Verify
Use the installed framework's supported task and fixture APIs. Prefer Python's asyncio documentation for exact semantics. Report unresolved failures and untested shutdown behavior; completed tasks alone do not prove safe cleanup.
04 同仓库更多 Skills
turnstile-spin
Set up Cloudflare Turnstile end-to-end in a project. Scan the codebase, create the widget via the Cloudflare API, embed it where user requests need bot verification (form submissions, SPA actions, API endpoints, download links, comment or vote submissions, etc.), wire canonical server-side siteverify in the customer's existing backend, validate, and persist the skill. Load this when a user asks to add Turnstile, set up CAPTCHA, protect a form or endpoint from bots, or fix a Turnstile integration. Uses Cloudflare's documented Turnstile contract.
frontend-design
Create distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
source-insights
Prepare inspected source evidence, preserve exact citations, and publish reviewed insights into an explicit local library.
skill-graph
Discover skills and workflows through independent lexical search, relationship paths and a terminal graph.
session-search
Recall public session turns with exact provenance, bounded excerpts and independent piped exports.
newsletter
Produce a private local AI newsletter from bounded public research, with cited deep dives, HTML/Markdown output, Dagu scheduling and seven-day retention.