Concurrency in network automation: threads, asyncio and safe limits
In short: Most time in a network script is spent waiting for devices to respond. Running tasks concurrently, using threads or asyncio, lets a script work on many devices at the same time and finish in minutes instead of hours. Use a sensible limit on workers, handle errors per device so one failure does not stop the rest, and avoid overloading authentication servers.
Why concurrency helps
Connecting, logging in and running a command can take several seconds per device, almost all of it waiting on the network. While waiting for one device, the script could be talking to others. This is called being I/O bound, and threads or asyncio handle it well.
Threads with ThreadPoolExecutor
import os
from concurrent.futures import ThreadPoolExecutor, as_completed
from netmiko import ConnectHandler
hosts = ["10.0.0.1", "10.0.0.2", "10.0.0.3"]
def get_version(host):
device = {"device_type": "cisco_ios", "host": host,
"username": os.environ["NET_USER"],
"password": os.environ["NET_PASS"]}
with ConnectHandler(**device) as conn:
return host, conn.send_command("show version | include Version")
results, errors = {}, {}
with ThreadPoolExecutor(max_workers=20) as pool:
futures = {pool.submit(get_version, h): h for h in hosts}
for fut in as_completed(futures):
host = futures[fut]
try:
h, output = fut.result()
results[h] = output
except Exception as exc:
errors[host] = str(exc)
print(f"{len(results)} ok, {len(errors)} failed")
Asyncio
Asyncio runs many tasks in a single thread, switching between them while they wait. Libraries such as Scrapli (async drivers), asyncssh and httpx support it. It scales to many connections with low overhead but needs async-aware libraries throughout.
Nornir
Nornir runs tasks against an inventory using a thread pool automatically (set num_workers in its runner configuration), and collects results and failures per host. For many teams it is the easiest way to get safe concurrency.
Choosing limits
- Start with 10 to 50 workers and measure.
- Consider TACACS+ or RADIUS servers: hundreds of logins in a second can overload them or trigger lockouts.
- Devices with slow CPUs or limited VTY lines may reject connections.
- For changes (not just reads), use smaller batches so problems are caught early.
Error handling
Catch errors for each device, record them and continue. At the end, report which devices succeeded and which failed, so you can retry the failures.
Real-world example: locked out by success
An engineer ran a new script with 300 threads. Within seconds, the TACACS+ servers rate-limited the logins, and many attempts failed, which then triggered account lockout for the automation user. Limiting workers to 30 kept logins at a level the servers handled easily, and the job still finished in a few minutes.
Practice
Why are network automation scripts described as I/O bound?
Why limit the number of concurrent workers?
What should a concurrent script do when one device fails?
Key takeaways
- Network scripts mostly wait, so concurrency gives large speed-ups.
- Use ThreadPoolExecutor, asyncio or Nornir's built-in workers.
- Limit workers to protect AAA servers and devices; use small batches for changes.
- Handle errors per device and report results clearly.
Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.