Free tech learning for everyone, from basics to pro

Network AutomationNetworking SecurityIntermediate

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.

Running a task on 500 devicesOne at a time10 seconds eachAbout 83 minutesSimple codeLow loadConcurrent, 50 workers10 seconds eachAbout 2 minutesNeeds careLimit workers
Figure 1. Network tasks spend most of their time waiting, so concurrency gives huge speed-ups.

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?
Most of their time is spent waiting for devices and the network, not computing.
Why limit the number of concurrent workers?
To avoid overloading AAA servers and devices, and to catch problems early during changes.
What should a concurrent script do when one device fails?
Record the error, continue with the other devices, and report failures at the end.

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.
Tracks your learning on this device only. See your progress on the learning paths.
Verified author✓ Valid
Issued to
Mohamed Fayas
Role
Founder of FM TecHub
Credentials
CCNABTEC HND Network EngineeringBEng (Hons) Computer Networking & Cloud Security
Issuer
FM TecHub · fmtechub.com

He writes practical, plain-English guides on networking, security and cloud.

More about the author →

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.

Email updates: coming soon

New notes, tools and security news from FM TecHub by email, explained simply. Free. We're not collecting email addresses yet; see our privacy policy.

PortServiceStatus
SMTP/25Email updatesopening soon

Meanwhile, stay connected: