- Home
- Skills
- 0xdarkmatter
- Claude Mods
- Python Async Patterns
python-async-patterns_skill
- Shell
8
GitHub Stars
1
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill 0xdarkmatter/claude-mods --skill python-async-patterns- SKILL.md4.2 KB
Overview
This skill provides practical asyncio patterns for concurrent Python programming, focused on real-world usage of coroutines, gathering, bounded concurrency, TaskGroup, timeouts, and aiohttp. It aims to help developers write safe, efficient async code and avoid common pitfalls like blocking the event loop or orphaned tasks.
How this skill works
The skill inspects code patterns and offers idiomatic implementations: concurrent execution with asyncio.gather, bounded concurrency via Semaphore, structured concurrency with TaskGroup (Python 3.11+), and timeout handling with asyncio.timeout. It highlights correct async I/O (aiohttp), async context managers, and anti-patterns such as blocking calls and lost tasks, plus references and scripts for deeper checks.
When to use it
- Implement concurrent HTTP clients or other I/O-bound workloads.
- Limit parallelism to avoid resource exhaustion (rate limits, DB connections).
- Migrate sync code to async or detect blocking calls in async functions.
- Adopt structured concurrency and timeouts for robust task lifecycles.
- Build producer-consumer pipelines or background workers with Queue and TaskGroup.
Best practices
- Never use blocking calls (time.sleep, requests) inside async functions—use awaitable alternatives or run_in_executor.
- Keep references to create_task results or use TaskGroup to avoid orphaned tasks and ensure cleanup.
- Use Semaphore or a bounded worker pool to control parallelism and protect external services.
- Prefer asyncio.timeout and TaskGroup (3.11+) for clear timeouts and structured concurrency.
- Use async context managers for resource acquisition and ensure proper closing of connections.
Example use cases
- Fetch hundreds of URLs concurrently while limiting concurrency to avoid rate limits using Semaphore + aiohttp.
- Process a queue of jobs with a TaskGroup ensuring all tasks finish or exceptions propagate cleanly.
- Wrap slow operations with asyncio.timeout to implement request-level timeouts and fallback handling.
- Scan a codebase for blocking calls in async functions using the provided script before deploying to production.
- Create a reusable async context manager for pooled connections and safe cleanup.
FAQ
Use asyncio.gather for simple independent tasks; use TaskGroup for structured concurrency when you want automatic cancellation, clearer lifecycle management, and better exception propagation (Python 3.11+).
How do I avoid blocking the event loop?
Replace blocking calls with awaitable equivalents (asyncio.sleep, aiohttp) or run CPU/blocking I/O in thread/process pools via run_in_executor or concurrent.futures. Scan code with the provided script to find offenses.
How do I limit concurrent requests?
Use asyncio.Semaphore to bound concurrency or implement a worker pool that consumes from an asyncio.Queue to control parallelism and resource usage.