subprocess hangs on reading stdout

Tim Arnold tim.arnold at
Wed Oct 14 18:45:14 CEST 2009

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:
                if finish < time.time():
                    print 'skipping' # this works ok

            s = ''
            if p:
                s = # trhis will hang occasionally
            if not s:

Any ideas? comments on code welcome also.
--Tim Arnold

More information about the Python-list mailing list