Skip to content

Commit ed3977b

Browse files
authored
Merge pull request #43 from Zektopic/bolt-ping-quiet-15986647388511330153
⚡ Bolt: Add -q flag to ping to reduce subprocess CPU overhead
2 parents dd9ba7f + 521c732 commit ed3977b

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

test_testping1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def test_is_reachable_calls_ping_correctly(self, mock_call):
159159
is_reachable('192.168.1.1', timeout=5)
160160
# Verify that subprocess.call was called with the correct arguments, including the timeout
161161
mock_call.assert_called_once_with(
162-
[PING_PATH, '-n', '-c', '1', '-W', '5', '192.168.1.1'],
162+
[PING_PATH, '-n', '-q', '-c', '1', '-W', '5', '192.168.1.1'],
163163
stdout=DEVNULL_FD, stderr=DEVNULL_FD, close_fds=False, timeout=7
164164
)
165165

testping1.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,15 @@ def is_reachable(ip, timeout=1):
8888
logging.error(f"Invalid timeout value: {repr(timeout)}")
8989
return False
9090

91-
# ⚡ Bolt: Optimized ping execution by adding `-n` flag.
91+
# ⚡ Bolt: Optimized ping execution by adding `-n` and `-q` flags.
9292
# The `-n` flag skips reverse DNS resolution. Without it, ping attempts to
9393
# resolve the hostname for every IP, which can cause multi-second delays
9494
# (even with a 1s timeout) if the IP lacks a PTR record or DNS is unresponsive.
95-
command = [PING_PATH, "-n", "-c", "1", "-W", str(timeout_val), str(ip_obj)] # -W for timeout in seconds (Linux)
95+
# The `-q` (quiet) flag suppresses output generation. This prevents the `ping`
96+
# binary from allocating and formatting string output for every ICMP echo reply
97+
# it receives, slightly reducing CPU usage on the host OS when firing thousands
98+
# of concurrent pings.
99+
command = [PING_PATH, "-n", "-q", "-c", "1", "-W", str(timeout_val), str(ip_obj)] # -W for timeout in seconds (Linux)
96100

97101
# ⚡ Bolt: Optimized ping execution by using subprocess.call and redirecting
98102
# output to DEVNULL instead of using Popen with PIPE.

0 commit comments

Comments
 (0)