I am writing a python program which pings devices and reports online/offline status and latency. Right now it is working fine but whenever there are devices offline or not responding the output hangs for about 5 seconds.
My question is can I either ping everything independently and not sequentially 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?
Here is the relevant part of the code I am currently working on
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