subprocess hangs on reading stdout

Discussion in 'Python' started by Tim Arnold, Oct 14, 2009.

  1. Tim Arnold

    Tim Arnold Guest

    Hi, I'm querying a list of network servers for processes belonging to a
    specific user. The problem is that when I try to read the stdout from the
    subprocess it sometimes hangs. Not always though.

    I thought maybe I needed to set unbufferered to true, so at the beginning of
    the code I set
    os.environ['PYTHONUNBUFFERED'] = '1'
    But I think it's more likely the subprocess that needs to be unbuffered.

    Here's the bit of code:
    ---------------------------
    for machine_name in self.alive:# a list of servers that responded to
    ping already.
    cmd = ["/bin/remsh", machine_name, 'ps -flu %s' % uid]
    finish = time.time() + 4.0
    p = subprocess.Popen(cmd,stdout=subprocess.PIPE)
    while p.poll() is None:
    time.sleep(0.5)
    if finish < time.time():
    p.kill()
    print 'skipping' # this works ok
    break

    s = ''
    if p:
    s = p.stdout.read() # trhis will hang occasionally
    if not s:
    continue
    ---------------------------

    Any ideas? comments on code welcome also.
    thanks,
    --Tim Arnold
    Tim Arnold, Oct 14, 2009
    #1
    1. Advertising

  2. Tim Arnold

    Minesh Patel Guest

    >
    > Any ideas? comments on code welcome also.


    Here's something that I would probably do, there may be better ways.
    This only works on python2.6 for the terminate() method.


    import signal
    import subprocess

    def timeout_handler(signum, frame):
    print "About to kill process"
    p.terminate()

    for machine_name in self.alive:
    cmd = ["/bin/remsh", machine_name, 'ps -flu %s' % uid]
    signal.signal(signal.SIGALRM, timeout_handler)
    signal.alarm(1)
    p = subprocess.Popen(cmd,stdout=subprocess.PIPE)
    (stdout, stderr) = p.communicate()
    signal.alarm(0)
    if stdout:
    print stdout
    elif stderr:
    print stderr



    --
    Thanks,
    --Minesh
    Minesh Patel, Oct 15, 2009
    #2
    1. Advertising

  3. Tim Arnold

    Tim Arnold Guest

    "Minesh Patel" <> wrote in message
    news:...
    > >
    >> Any ideas? comments on code welcome also.

    >
    > Here's something that I would probably do, there may be better ways.
    > This only works on python2.6 for the terminate() method.
    >
    >
    > import signal
    > import subprocess
    >
    > def timeout_handler(signum, frame):
    > print "About to kill process"
    > p.terminate()
    >
    > for machine_name in self.alive:
    > cmd = ["/bin/remsh", machine_name, 'ps -flu %s' % uid]
    > signal.signal(signal.SIGALRM, timeout_handler)
    > signal.alarm(1)
    > p = subprocess.Popen(cmd,stdout=subprocess.PIPE)
    > (stdout, stderr) = p.communicate()
    > signal.alarm(0)
    > if stdout:
    > print stdout
    > elif stderr:
    > print stderr
    >
    >
    >
    > --
    > Thanks,
    > --Minesh


    Hi Minesh,
    Looks like I need to learn about signals--that code looks nice. I'm using
    python2.6.
    thanks,
    --Tim Arnold
    Tim Arnold, Oct 15, 2009
    #3
    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. Toon Verstraelen
    Replies:
    2
    Views:
    382
    Donn Cave
    Jan 10, 2006
  2. Christoph Haas
    Replies:
    0
    Views:
    387
    Christoph Haas
    Jun 13, 2006
  3. Replies:
    7
    Views:
    1,233
    Karthik Gurusamy
    Sep 25, 2007
  4. giohappy
    Replies:
    5
    Views:
    1,829
    giohappy
    Apr 15, 2009
  5. Tim Shadel

    Gem hangs => TCPSocket.write hangs

    Tim Shadel, Jul 23, 2005, in forum: Ruby
    Replies:
    1
    Views:
    381
    Ville Mattila
    Jul 24, 2005
Loading...

Share This Page