Use this when using threads for I/O-bound automation.
Threading
Threading runs several threads in one process sharing memory, useful for I/O-bound automation like SSH to many devices. Shared data must be protected, and in Python CPU-bound work does not speed up.
1. Threads suit I/O-bound work (APIs, SSH).
2. Bound pool size.
3. Avoid shared mutable state or lock it.
4. Collect exceptions from futures.
5. Python GIL limits CPU-bound threads.
tasks → thread pool → results
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Verification path Verify boundary
The negative fixture should deliberately exercise risks such as ambiguous target identity, stale inputs, partial success, unsafe retry, permission drift and confusing tool completion with target-evidence of the actual state, choosing the cases most relevant to shared-memory concurrency, synchronization/locks, thread lifecycle, exception propagation and avoiding race-prone side effects.
Step 2 Common mistakes Narrow the fault
- Shared variables without locks.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- Good for SSH to many devices.
- Protect shared data.
- Python GIL limits CPU work.
- ThreadPoolExecutor(max_workers=N) — bounded threads
- Thread-safe data structures — safety
- GIL awareness (I/O-bound OK) — performance
ReferenceImportant terms & boundaries1 section · depth on demand
In depth
Threading runs several tasks concurrently inside one process. In Python, threads take turns executing Python code, but they release the interpreter while waiting for network or disk I/O — exactly what automation spends most of its time doing. That makes thread pools an easy way to talk to many devices or APIs at once.
from concurrent.futures import ThreadPoolExecutor, as_completed
with ThreadPoolExecutor(max_workers=20) as pool:
futures = {pool.submit(get_version, d): d for d in devices}
for f in as_completed(futures):
dev = futures[f]
try: print(dev, f.result())
except Exception as e: print(dev, "FAILED", e)
Shared data between threads (lists, counters, files) needs locks or thread-safe structures. Netmiko and requests sessions should not be shared across threads — create one per thread.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Parallelism & Concurrency > Threading. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://docs.python.org/3/library/concurrent.futures.html · confidence 94%Deep reference
Healthy vs suspicious evidence
Output interpretation
For shared-memory concurrency, synchronization/locks, thread lifecycle, exception propagation and avoiding race-prone side effects, prefer typed fields and stable IDs over human presentation text.