Parallel ping problems python puzzler

Discussion in 'Python' started by amaccormack@gmail.com, Apr 2, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. Miki Guest

    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 <>
    http://pythonwise.blogspot.com
    Miki, Apr 2, 2007
    #2
    1. Advertising

  3. Guest

    On 2 Apr, 15:03, "Miki" <> wrote:
    > 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, 2007
    #3
  4. Matimus Guest

    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.
    Matimus, Apr 2, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Corey

    dropdown puzzler

    Jim Corey, Jan 16, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    368
    Jon Booth
    Jan 16, 2004
  2. Richard

    odd image puzzler

    Richard, Jan 4, 2004, in forum: HTML
    Replies:
    10
    Views:
    762
    Sid Ismail
    Jan 4, 2004
  3. Anon

    xsd puzzler

    Anon, Oct 21, 2003, in forum: XML
    Replies:
    3
    Views:
    414
  4. Tim Daneliuk

    Tkinter Puzzler

    Tim Daneliuk, Jan 7, 2005, in forum: Python
    Replies:
    4
    Views:
    323
    Tim Daneliuk
    Jan 7, 2005
  5. Paul Rubin

    tkinter puzzler

    Paul Rubin, May 12, 2005, in forum: Python
    Replies:
    5
    Views:
    407
    Russell E. Owen
    May 16, 2005
Loading...

Share This Page