Python asyncio Semaphore Bounded-Concurrency Review Prompt
Review an asyncio script that fans out work to find unbounded concurrency, then redesign it with a semaphore-bounded task pool, proper cancellation, backpressure, and clean shutdown so it can't overwhelm downstreams.
- Target user
- Engineers writing async Python automation that calls APIs or services at scale
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a senior async Python engineer who has debugged production incidents caused by an innocent-looking `asyncio.gather` opening ten thousand connections at once. I will provide: - The async code (the fan-out loop and the per-item coroutine) - The downstream limits (rate caps, connection pool size, target tolerance) - How the script is launched and how failures should be handled Your job: 1. **Find the unbounded points** — identify every place that schedules N tasks with no cap (`gather` over a large list, `create_task` in a loop, unbounded queues) and explain the concrete failure mode it causes. 2. **Bound the concurrency** — introduce an `asyncio.Semaphore` (or a worker-pool/`TaskGroup` pattern) sized to the real downstream limit, and show the corrected fan-out with backpressure. 3. **Get cancellation right** — ensure exceptions in one task don't strand others; use `TaskGroup` or explicit gather-with-cancel so a failure cancels siblings cleanly and doesn't leave orphaned tasks. 4. **Manage shared clients** — share one connection-pooled client (httpx/aiohttp) instead of one per task, set per-request timeouts, and close it in a `finally`/async context manager. 5. **Handle partial failure** — collect per-item results vs errors so the script reports what succeeded and exits with a meaningful code instead of dying on the first exception. 6. **Shut down cleanly** — install signal handling so SIGINT/SIGTERM cancels in-flight tasks, awaits them, and closes the loop without "Task was destroyed but it is pending" warnings. Output as: a findings list (line refs + failure mode), the refactored async code, and a short note on how to size the semaphore. Never set the concurrency bound higher than the smallest downstream limit — match it, and prefer too low over too high.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Asyncio TaskGroup Structured-Concurrency Python Script Prompt
Write a Python 3.11+ automation script that fans work out with asyncio.TaskGroup so failures cancel siblings cleanly, errors aggregate into an ExceptionGroup, and no task is ever silently orphaned.
-
Python asyncio.create_subprocess_exec Fan-Out Prompt
Run many external commands concurrently under asyncio with bounded concurrency, captured output, and per-command timeouts.
-
Python Token Bucket Rate Limiter Prompt
Implement a correct, thread-safe (and asyncio-friendly) token bucket rate limiter in Python to throttle outbound API calls, respect provider quotas, and smooth bursts — with tests and a clean decorator/context-manager API.
-
Async Concurrent HTTP Poller with asyncio and httpx Prompt
Build a fast, bounded-concurrency async poller/fetcher that hits many endpoints with asyncio and httpx, with rate limiting, retries, timeouts, and structured results.
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.