Python 子进程调用效率
Python subprocess call efficiency
我正在编写一个 python 程序,它可以 ping 设备并报告 online/offline 状态和延迟。现在它工作正常,但只要有设备离线或不响应,输出就会挂起大约 5 秒。
我的问题是我可以独立地而不是顺序地 ping 所有东西 and/or 我可以在子进程上设置某种时间过滤器吗,如果在 ~100-200 毫秒后没有更新,它会移动继续下一个?
这是我目前正在处理的代码的相关部分
for item in lines:
#remove whitespaces, etc from item.
hostname = item.rstrip()
#Run ping and return output to stdout.
#subprocess.Popen runs cmdline ping, pipes the output to stdout. .stdout.read() then reads that stream data and assigns it to the ping_response variable
ping_response = subprocess.Popen(["ping", hostname, "-n", '1'], stdout=subprocess.PIPE).stdout.read()
word = "Received = 1"
word2 = "Destination host unreachable."
#Regex for finding the time values and inputting them in to a list.
p = re.compile(ur'(?<=time[<=])\S+')
x = re.findall(p, ping_response)
if word2 in ping_response:
print "Destination Unreachable"
elif word in ping_response:
print "%s is online with latency of " % hostname +x[0]
else:
print "%s is offlineOffline" % hostname
My question is can I either ping everything independently and not sequentially
当然可以。该问题有多种解决方案,包括 threading
和 multiprocessing
模块。
and/or can I set a time filter of some sort on the subprocess so that, if things aren't updated after ~100-200ms it moves on to the next?
您实际上可以在 ping
本身上设置超时,至少是 Linux 版本,使用 -W
选项:
-W timeout
Time to wait for a response, in seconds. The option affects only
timeout in absence of any responses, otherwise ping waits for
two RTTs.
Ping
具有超时功能,这将有助于提高脚本的效率。
-W waittime
Time in milliseconds to wait for a reply for each packet sent. If a reply arrives later, the packet
is not printed as replied, but considered as replied when calculating statistics.
此外,here 是其他一些可以有效 ping 的实用程序。
我正在编写一个 python 程序,它可以 ping 设备并报告 online/offline 状态和延迟。现在它工作正常,但只要有设备离线或不响应,输出就会挂起大约 5 秒。
我的问题是我可以独立地而不是顺序地 ping 所有东西 and/or 我可以在子进程上设置某种时间过滤器吗,如果在 ~100-200 毫秒后没有更新,它会移动继续下一个?
这是我目前正在处理的代码的相关部分
for item in lines:
#remove whitespaces, etc from item.
hostname = item.rstrip()
#Run ping and return output to stdout.
#subprocess.Popen runs cmdline ping, pipes the output to stdout. .stdout.read() then reads that stream data and assigns it to the ping_response variable
ping_response = subprocess.Popen(["ping", hostname, "-n", '1'], stdout=subprocess.PIPE).stdout.read()
word = "Received = 1"
word2 = "Destination host unreachable."
#Regex for finding the time values and inputting them in to a list.
p = re.compile(ur'(?<=time[<=])\S+')
x = re.findall(p, ping_response)
if word2 in ping_response:
print "Destination Unreachable"
elif word in ping_response:
print "%s is online with latency of " % hostname +x[0]
else:
print "%s is offlineOffline" % hostname
My question is can I either ping everything independently and not sequentially
当然可以。该问题有多种解决方案,包括 threading
和 multiprocessing
模块。
and/or can I set a time filter of some sort on the subprocess so that, if things aren't updated after ~100-200ms it moves on to the next?
您实际上可以在 ping
本身上设置超时,至少是 Linux 版本,使用 -W
选项:
-W timeout
Time to wait for a response, in seconds. The option affects only
timeout in absence of any responses, otherwise ping waits for
two RTTs.
Ping
具有超时功能,这将有助于提高脚本的效率。
-W waittime
Time in milliseconds to wait for a reply for each packet sent. If a reply arrives later, the packet
is not printed as replied, but considered as replied when calculating statistics.
此外,here 是其他一些可以有效 ping 的实用程序。