467,074 Members | 912 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,074 developers. It's quick & easy.

Parallel ping problems python puzzler

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
  • viewed: 1591
Share:
3 Replies
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
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
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.

Similar topics

2 posts views Thread by Alberto Vera | last post: by
reply views Thread by Emile van Sebille | last post: by
2 posts views Thread by Count László de Almásy | last post: by
5 posts views Thread by SolaFide | last post: by
43 posts views Thread by parallelpython@gmail.com | last post: by
7 posts views Thread by Linus Cohen | last post: by
1 post views Thread by Mauro \Baba\ Mascia | last post: by
1 post views Thread by Karl Kobata | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.