473,554 Members | 3,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

subprocess -popen - reading stdout from child - hangs

Let's say I have this Python file called loop.py:

import sys
print 'hi'
sys.stdout.flus h()
while 1:
pass

And I want to call it from another Python process and read the value
'hi'. How would I do it?

So far I have tried this:
>>proc = subprocess.Pope n('python /home/chiefinnovator/loop.py',shell= True,stdin=subp rocess.PIPE,std out=subprocess. PIPE)
proc.stdout.r ead()
But it just hangs at read()

proc.communicat e() also just hangs. What am I doing wrong? Please
advise.

Thanks,

Greg

Sep 23 '07 #1
6 17527
On Sep 22, 11:28 pm, "gregpin...@gma il.com" <gregpin...@gma il.com>
wrote:
Let's say I have this Python file called loop.py:

import sys
print 'hi'
sys.stdout.flus h()
while 1:
pass

And I want to call it from another Python process and read the value
'hi'. How would I do it?

So far I have tried this:
>proc = subprocess.Pope n('python /home/chiefinnovator/loop.py',shell= True,stdin=subp rocess.PIPE,std out=subprocess. PIPE)
proc.stdout.re ad()

But it just hangs at read()

proc.communicat e() also just hangs. What am I doing wrong? Please
advise.
Well, using this subclass of subprocess.Pope n fixes the problem
(http://aspn.activestate.com/ASPN/Coo.../Recipe/440554)

I don't understand how it works though. Would anyone mind
explaining? I'm thinking there's something fundamental about Unix
processes I'm not understanding.

-Greg

Sep 23 '07 #2
On Sep 22, 8:28 pm, "gregpin...@gma il.com" <gregpin...@gma il.com>
wrote:
Let's say I have this Python file called loop.py:

import sys
print 'hi'
sys.stdout.flus h()
Add sys.stdout.clos e()
while 1:
pass

And I want to call it from another Python process and read the value
'hi'. How would I do it?

So far I have tried this:
>proc = subprocess.Pope n('python /home/chiefinnovator/loop.py',shell= True,stdin=subp rocess.PIPE,std out=subprocess. PIPE)
proc.stdout.re ad()

But it just hangs at read()

proc.communicat e() also just hangs. What am I doing wrong? Please
advise.
Since your loop.py is still alive and hasn't closed its stdout, the
caller continues to wait for EOF (it doesn't know if loop.py is done
generating all its output)

Karthik
>
Thanks,

Greg

Sep 23 '07 #3
In message <11************ **********@19g2 000hsx.googlegr oups.com>,
gr********@gmai l.com wrote:
Let's say I have this Python file called loop.py:

import sys
print 'hi'
sys.stdout.flus h()
while 1:
pass

And I want to call it from another Python process and read the value
'hi'. How would I do it?

So far I have tried this:
>>>proc = subprocess.Pope n('python
/home/chiefinnovator/loop.py',shell= True,stdin=subp rocess.PIPE,std out=subprocess. PIPE)
proc.stdout. read()

But it just hangs at read()
That's because you didn't tell it how much to read, so by default it tries
to read until EOF <http://docs.python.org/lib/bltin-file-objects.html>. But
since the subprocess is still running and hasn't closed its stdout, the
pipe has not reached EOF.
Sep 24 '07 #4
In message <11************ *********@d55g2 000hsg.googlegr oups.com>,
gr********@gmai l.com wrote:
Well, using this subclass of subprocess.Pope n fixes the problem
(http://aspn.activestate.com/ASPN/Coo.../Recipe/440554)

I don't understand how it works though. Would anyone mind
explaining? I'm thinking there's something fundamental about Unix
processes I'm not understanding.
The "select" calls are checking that the subprocess is ready to read before
writing to its stdin, or that it has something to write before reading from
its stdout.

The O_NONBLOCK fcntl simply says not to wait if there is nothing more to be
read, just return what was already read.
Sep 24 '07 #5
On Sep 23, 2:58 am, Karthik Gurusamy <kar1...@gmail. comwrote:
On Sep 22, 8:28 pm, "gregpin...@gma il.com" <gregpin...@gma il.com>
wrote:
Let's say I have this Python file called loop.py:
import sys
print 'hi'
sys.stdout.flus h()

Add sys.stdout.clos e()
Adding sys.stdout.clos e() and removing sys.stdout.flus h() seems to
make it work. But can the while loop still use sys.stdout later on?
Do I have to reopen it?

Thanks,

Greg
Sep 24 '07 #6
On Sep 24, 2:22 pm, "gregpin...@gma il.com" <gregpin...@gma il.com>
wrote:
On Sep 23, 2:58 am, Karthik Gurusamy <kar1...@gmail. comwrote:
On Sep 22, 8:28 pm, "gregpin...@gma il.com" <gregpin...@gma il.com>
wrote:
Let's say I have this Python file called loop.py:
import sys
print 'hi'
sys.stdout.flus h()
Add sys.stdout.clos e()

Adding sys.stdout.clos e() and removing sys.stdout.flus h() seems to
make it work. But can the while loop still use sys.stdout later on?
Do I have to reopen it?
Once you close a file-object, you cannot use it. You'll get exception
if you try.
I quickly tried the fdopen on id 1 (stdout in unix like OS) and it
seems to work
>>import sys
sys.stdout.cl ose()
import os
print 'hi'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file
>>sys.stdout=os .fdopen(1, 'w')
print 'hi'
hi
>>>
BUT you may want to re-think your design. Note that your caller
process will anyway stop reading further from your loop.py process
the moment it sees the "first" EOF. So even if you enable loop.py to
generate more output (thru' the above fdopen), the caller process is
not going to see the newly generated data.

If your requirement is to collect all output from loop.py then you
can't do it if loop.py has an infinite loop across its data generation
points (ie it can generate data after the infinite loop -- which
anyway doesn't sense).

If all you want is not to get blocked, try one of the select solutions
or read a small amount at a time (which you can guarantee to be
available). Yet another solution would be you could set up an alarm
and get out of the blocking read if the alarm fires.

Karthik
>
Thanks,

Greg

Sep 25 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1381
by: calmar | last post by:
Hi all, first of all, I have to say I have hardly no knowledge about that issue. on the program there, I have it like that: ,------------------------------------------------------- | obj = unicode(newcomment, 'utf-8')
0
2430
by: Christoph Haas | last post by:
Evening, I'm having trouble with running a process through Python 2.4's subprocess module. Example code: ======================================================== def run(command): run = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # Wait for the process to return
13
5093
by: bayer.justin | last post by:
Hi, I am trying to communicate with a subprocess via the subprocess module. Consider the following example: <subprocess.Popen object at 0x729f0> Here hey is immediately print to stdout of my interpreter, I did not type in the "hey". But I want to read from the output into a string, so I do
12
10973
by: Eric_Dexter | last post by:
I am trying to modify a programming example and I am coming up with two problems... first is that I can't seem to pass along the arguments to the external command (I have been able to do that with the old module and cmd is the command I wish to try) all the output seems to be returned as one line (at least when I run the program in spe). ...
1
3976
by: Mrown | last post by:
Hi, I'm currently writing a python program that relies on a CLI program. What I'm currently doing is using subprocess.Popen on Python 2.5.1. Here's the line that I'm currently running: child = subprocess.Popen(, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) The problem is that although the above works, a CMD...
1
1934
by: Marko Rauhamaa | last post by:
This tiny program hangs: ======================================================================== #!/usr/bin/env python import subprocess a = subprocess.Popen('cat',shell = True,stdin = subprocess.PIPE, stdout = subprocess.PIPE) b = subprocess.Popen('cat >/dev/null',shell = True,stdin = a.stdout) a.stdin.close() b.wait() # hangs
7
4903
by: skunkwerk | last post by:
Hi, i'm trying to call subprocess.popen on the 'rename' function in linux. When I run the command from the shell, like so: rename -vn 's/\.htm$/\.html/' *.htm it works fine... however when I try to do it in python like so: p = subprocess.Popen(,stdout=subprocess.PIPE,stderr=subprocess.PIPE) print p.communicate()
0
2077
by: Christian Heimes | last post by:
Kenneth McDonald wrote: The subprocess module has already an API method for your use case. The communicate() method of a subprocess.Popen instance takes an optional stdin string and returns the stdout and stderr output as strings. The method uses the best low level functions possible (select() on Unix, threads on Windows). Christian
5
4117
by: thedsadude | last post by:
Hello, I'm launching a script as follows: <code> p = subprocess.Popen() p.wait() </code> If p.py writes to sys.stdout, then it is shown on the console.
0
7581
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8018
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6123
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5423
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5142
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3545
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3533
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2006
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
823
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.