Backend Engineer: Non-Blocking Network Diagnostics: Asynchronous Ping Engines in Python at the Edge
2021-05-02 02:00:00+00:00
In telemetry platforms managing thousands of remote device nodes, monitoring host availability requires running ping checks continuously. If a gateway loops through a list of devices using standard synchronous ICMP sockets or subprocess calls, a single offline device will block the execution thread during the timeout. Running checks in parallel using threads is resource-heavy. The correct way to handle large-scale network diagnostics is to use asynchronous I/O.
By leveraging Python's asyncio and custom socket handlers, we can build a non-blocking ping engine that monitors hundreds of endpoints in parallel.
1. Designing the Asynchronous Ping Runner
We write a script using asyncio subprocess queues to execute network pings concurrently without locking the main event loop:
# async_ping.py
import asyncio
import sys
async def ping_host(host):
"""Asynchronously ping a host using system utilities"""
# Launch ping process non-blockingly
cmd = ["ping", "-c", "2", "-W", "2", host]
proc = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await proc.communicate()
if proc.returncode == 0:
print(f"SUCCESS: {host} is online.")
return host, True
else:
print(f"FAILED: {host} is unreachable.")
return host, False
async def monitor_network(hosts):
# Run ping jobs concurrently
tasks = [ping_host(h) for h in hosts]
results = await asyncio.gather(*tasks)
return results
if __name__ == "__main__":
device_ips = ["8.8.8.8", "8.8.4.4", "192.168.1.1", "10.0.0.1", "1.1.1.1"]
# Run the async loop
asyncio.run(monitor_network(device_ips))
2. Scaling Core Diagnostics
Async I/O scales gracefully: pinger threads are replaced by lightweight tasks managed in a single OS thread. This allows a low-power edge node (like an IoT gateway) to monitor hundreds of device connections without consuming system memory, maintaining edge responsiveness.
Non-Blocking ICMP & TCP Socket Probing with Python Asyncio
Run hundreds of concurrent network reachability, DNS resolution, and packet-loss probes inside a single asyncio event loop without spawning blocking subprocesses.
About the Author — Hire Aldian Fazrihady (Cloud Engineer, GCP Professional Cloud Architect, GCP Professional Data Engineer, Backend, AI & Full Stack Engineer)
Aldian Fazrihady is a Top-Rated Senior Cloud Engineer, GCP Professional Cloud Architect (Google Cloud Certified), GCP Professional Data Engineer (Google Cloud Certified), Backend Engineer (Python, Django, FastAPI, Go, PostgreSQL), AI Application Engineer (LLMs, RAG, MCP, Google ADK, LangGraph), and Full Stack Engineer (React, Next.js, TypeScript) with 25+ years of production software engineering experience.