Parallel ping problems python puzzler

A

amaccormack

I wrote a quick script to check the "up-ness" of a list of machines,
and timeout after 1 second. However, with a lot of timeouts, the
script takes a logn time, so I thought to parallelise it. However, as
soon as I do, the pings that do not get a response never return, so
their threads block forever and the program hangs. Environment is:
Python 2.3.3 (#1, Jan 5 2005, 15:24:27) [GCC 3.3.3 (SuSE Linux)] on
linux2 (running on SLES9)


pinglist=[]
class testit(Thread):
def __init__ (self,ip):
Thread.__init__(self)
self.ip = ip
self.status = -1
def run(self):
# -w 1 option to ping makes it timeout after 1 second
pingcmd="/bin/ping -c 2 -q -i 0.3 -w 1 %s >/dev/null" % ip
self.status = os.system(pingcmd)

def serping(ip):
pingcmd="/bin/ping -c 2 -q -i 0.3 -w 1 %s >/dev/null" % ip
os.system(pingcmd)

for machname in machlist:
#serping(machname) # this works in serial, and works
current = testit(machname) # this works in parallel, and
doesn't work
pinglist.append(current)
current.start()

# Wait for all pings to pong
for pingle in pinglist:
pingle.join()


Anyone got an idea what's going on? Is it the way that the ping
timeout works in SuSE is not thread-safe?
 
M

Miki

Hello,
def run(self):
# -w 1 option to ping makes it timeout after 1 second
pingcmd="/bin/ping -c 2 -q -i 0.3 -w 1 %s >/dev/null" % ip
Not sure, but "ip" should be "self.ip", this might cause the problem.


HTH,
 
A

amaccormack

Hello,


Not sure, but "ip" should be "self.ip", this might cause theproblem.

Sorry, that was my bad cutting-and-pasting to make a shorter example,
it IS self.ip in the real code. The REAL code works fine parallel or
serial as long as everything responds to the ping, but as soon as any
devices do not respond, it hangs in the threaded version.
 
M

Matimus

I wouldn't use threads for system calls. Checkout the subprocess
module instead. You can run multiple pipes at the same time
(subprocess.Popen). The python documentation for subprocess is pretty
good. There are a few examples. Actually, you don't even _need_ the
subprocess module, you can use os.popen for similar functionality.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top