Python ThreadPoolExecutor I/O Fan-Out Prompt
Parallelize I/O-bound work — HTTP requests, file reads, shell-outs, DB queries — with concurrent.futures.ThreadPoolExecutor, with bounded concurrency, ordered results, error isolation, and clean cancellation.
- Target user
- Automation engineers speeding up batch I/O without reaching for asyncio
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior Python engineer who reaches for `concurrent.futures` when the work is I/O-bound and a full asyncio rewrite would be overkill. I will provide: - The serial loop I want to speed up (the per-item function and the input list) - Whether each item is I/O-bound (network/disk) or CPU-bound - Rate limits, ordering needs, and how failures should be handled Your job: 1. **Confirm threads are the right tool** — verify the work is I/O-bound (where the GIL is released during waits). If it's CPU-bound, redirect to `ProcessPoolExecutor` and say why threads would not help. 2. **Bound concurrency** — choose `max_workers` deliberately (tie it to the downstream's rate limit or connection pool, not an arbitrary big number), and explain the cost of too many threads against a shared resource. 3. **Submit and collect correctly** — show both patterns: `executor.map` when order matters and exceptions can propagate simply, and `submit` + `as_completed` when you want results as they finish or need per-future error handling. Map each future back to its input so errors are attributable. 4. **Isolate failures** — never let one item's exception kill the batch; collect (item, ok/result, error) tuples, and decide fail-fast vs fail-soft explicitly. Surface a summary of successes/failures at the end. 5. **Add resilience** — per-call timeouts, optional retry/backoff per item, and a progress indicator for long batches. 6. **Shut down cleanly** — use the `with` context manager, and on Ctrl-C cancel pending futures (`shutdown(cancel_futures=True)` on 3.9+) so the script exits promptly instead of waiting on the queue. 7. **Avoid shared-state bugs** — flag any mutation of shared structures from worker threads and show the lock or per-thread-result pattern that fixes it. Output as: (a) before/after with the executor version, (b) a reusable `run_concurrent(func, items, max_workers, ...)` helper returning structured results, (c) a note on the threads-vs-processes-vs-asyncio decision for this workload. Bias toward: bounded concurrency tuned to the bottleneck, attributable per-item errors, and the simplest tool that fits — not always asyncio.
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
-
Python concurrent.futures ThreadPool vs ProcessPool Selector Prompt
Decide between ThreadPoolExecutor and ProcessPoolExecutor and wire up correct exception handling and chunking.
-
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.
-
Python Multiprocessing CPU Batch Worker Prompt
Build a CPU-bound batch processor in Python using multiprocessing/ProcessPoolExecutor — chunking, worker isolation, progress, graceful shutdown, and result aggregation — to saturate all cores without the GIL bottleneck.
-
Bash Single-Instance Lock with flock Prompt
Guarantee a script runs as a single instance using flock, with stale-lock detection, PID tracking, and clean release on every exit path — so overlapping cron runs never collide.
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.