Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > subprocess hangs on reading stdout

Reply
Thread Tools

subprocess hangs on reading stdout

 
 
Tim Arnold
Guest
Posts: n/a
 
      10-14-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:
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


 
Reply With Quote
 
 
 
 
Minesh Patel
Guest
Posts: n/a
 
      10-15-2009
>
> 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
 
Reply With Quote
 
 
 
 
Tim Arnold
Guest
Posts: n/a
 
      10-15-2009
"Minesh Patel" <> wrote in message
news:mailman.1408.1255583431.2807.python-...
> >
>> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
subprocess: reading from stdout hangs process termination, waitingfor ENTER keyboard signal giohappy Python 5 04-15-2009 10:24 AM
subprocess -popen - reading stdout from child - hangs gregpinero@gmail.com Python 7 09-25-2007 02:42 AM
Correctly reading stdout/stderr from subprocess Christoph Haas Python 0 06-13-2006 10:20 PM
[Subprocess/Windows] subprocess module under Windows 98 Andreas Jung Python 2 11-02-2005 05:41 PM
Clear hangs up - & hangs up - & hangs up Sue Bilstein NZ Computing 26 03-07-2004 01:33 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57