By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,209 Members | 1,706 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,209 IT Pros & Developers. It's quick & easy.

Parallel ping problems python puzzler

P: n/a
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?

Apr 2 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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,
--
Miki <mi*********@gmail.com>
http://pythonwise.blogspot.com

Apr 2 '07 #2

P: n/a
On 2 Apr, 15:03, "Miki" <miki.teb...@gmail.comwrote:
Hello,
def run(self):
# -w 1 option topingmakes ittimeoutafter 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 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.

Apr 2 '07 #3

P: n/a
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.

Apr 2 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.