473,386 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

correct way of running a sub-process

What is the (hopefully unique) obvious way of runnign a sub-process if I
want to get the exit code and input without resorting to multi-threading?

It seems like I should be able to do the following:

foo = popen2.Popen3(cmd)
foo.wait()
foo.fromchild.read()

But, it seems like on my system (a Sun Ultra 30 running Solaris 8) this
will hang if the input happens to have more than about 12k of text. I'm
guessing this is a buffersize issue, but it would be a mistake to assume
at compile time an output size for the child process.

Couldn't wait be modified to actually wait until a process exits by
buffering?

While we're at it, is there any way to read/wait with a time-out? I.e.,
read more than 0 bytes, unless it takes more than a time-out value, so I
can run sub-processes that terminate?

-Dan

Jul 18 '05 #1
4 1788
On Thu, Feb 12, 2004 at 06:10:01AM -0800, Daniel Timothy Bentley wrote:
What is the (hopefully unique) obvious way of runnign a sub-process if I
want to get the exit code and input without resorting to multi-threading?

It seems like I should be able to do the following:

foo = popen2.Popen3(cmd)
foo.wait()
foo.fromchild.read()

But, it seems like on my system (a Sun Ultra 30 running Solaris 8) this
will hang if the input happens to have more than about 12k of text. I'm
guessing this is a buffersize issue, but it would be a mistake to assume
at compile time an output size for the child process.

Couldn't wait be modified to actually wait until a process exits by
buffering?

While we're at it, is there any way to read/wait with a time-out? I.e.,
read more than 0 bytes, unless it takes more than a time-out value, so I
can run sub-processes that terminate?


(Untested)

from twisted.internet import protocol, reactor

class PrintyProtocol(protocol.ProcessProtocol):
bytes = ''
errBytes = ''

def outReceived(self, bytes):
self.bytes += bytes

def errReceived(self, bytes):
self.errBytes += bytes

def processEnded(self, reason):
print 'Process done. Got:', repr(self.bytes), repr(self.errBytes)
reactor.stop()

reactor.spawnProcess(PrintyProtocol(), cmd, args=(cmd,))
reactor.run()

Jp

Jul 18 '05 #2
Am Thu, 12 Feb 2004 06:10:01 -0800 schrieb Daniel Timothy Bentley:
What is the (hopefully unique) obvious way of runnign a sub-process if I
want to get the exit code and input without resorting to multi-threading?

It seems like I should be able to do the following:

foo = popen2.Popen3(cmd)
foo.wait()
foo.fromchild.read()


I do it like this:

def shell_command(cmd):
# There mustnot be output to stdout or stderr
# otherwise an exception is raised

p=popen2.Popen4(cmd) # read stdout and stderr
output=p.fromchild.read()
ret=p.wait()
if ret or output:
raise("Error in shell_command '%s': ret=%s output='%s'" %(
cmd, ret, output))
Jul 18 '05 #3
Correct me if I'm wrong, but can't this not catch all the output? Or is
read in python guaranteed to return all the data that can ever be returned
(unlike the C library function)?

-D
"Thomas Guettler" <gu*****@thomas-guettler.de> wrote in message
news:pa****************************@thomas-guettler.de...
def shell_command(cmd):
# There mustnot be output to stdout or stderr
# otherwise an exception is raised

p=popen2.Popen4(cmd) # read stdout and stderr
output=p.fromchild.read()
ret=p.wait()
if ret or output:
raise("Error in shell_command '%s': ret=%s output='%s'" %(
cmd, ret, output))

Jul 18 '05 #4
In article <c0**********@news.Stanford.EDU>,
"Daniel Danger Bentley" <db******@stanford.edu> wrote:
Correct me if I'm wrong, but can't this not catch all the output? Or is
read in python guaranteed to return all the data that can ever be returned
(unlike the C library function)?


You are indeed wrong, misled by the name - the C library function
in question is fread(3), which does read all. Not like the read(2)
system call, which is posix.read (a.k.a. os.read)

Back to the original question, I think the simplest way to get
status and output is

fp = os.popen(cmd, 'r')
output = fp.read()
status = os.WEXITSTATUS(fp.close())

Now if you want a timeout, you'll have to do it yourself, and
of course the file object isn't the way to go because of its
underlying buffered read. You can turn off buffering, but then
you're looking at one system call per byte to implement readline
et al., so it's not a generally attractive solution. Better to
get the file descriptor (fileobject.fileno()), and probably use
select on it. You might also want to do that if you end up
reading from two different pipes for stderr and stdout, because
that can lead to the same buffer deadlock you're running into
now - if I'm reading from stdout while the process writes to
stderr, or vice versa, it can fill the pipe and block waiting
for me to empty it, which I won't do because I'm stuck on stdout.

Donn Cave, do**@u.washington.edu
Jul 18 '05 #5

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

Similar topics

5
by: Shalen chhabra | last post by:
Hey, Can anyone give me a snippet for running a python program over all the files in the directory. For ex: I have ten files in a directory and I want to run a python program against all of...
1
by: Craig Ringer | last post by:
Hi folks I'm a bit of a newbie here, though I've tried to appropriately research this issue before posting. I've found a lot of questions, a few answers that don't really answer quite what I'm...
4
by: Niyazi | last post by:
Hi I am trying to insert some value into SQL Server 2000 tables and I am keep getting the "Input string was not in a correct format" error. When user fills the Form it updates the table call...
2
by: Sara | last post by:
Hi - Maybe it's my code, or maybe it's that I am not coding the Call to a sub in my code properly. Whichever, I am getting the error "Invalid Use of Null" when running this code. I do see my...
1
by: Dr. Zharkov | last post by:
Hello. A problem in the following. In VB .NET 2003 I create the project and in file Form1.vb is written down the following code: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As...
0
by: sehguh | last post by:
Hiya Folks, I am Currently using windows xp. Also using Visual Web Developer 2005 and Microsoft Sql server 2005. The main page consists of an aspx page and a master page. The page also...
1
by: sehguh | last post by:
Hello folks I have recently been studying a book called "sams teach yourself asp.net 2.0 in24 hours by scott mitchell. I have reached page 614 but when i tried to run an asp page called...
1
by: krndhi1983 | last post by:
Hi to All, I am using Windows XP running IIS 6.0.I am developing a project using vb.net 1.1 with SQL Server 2000.It is a mobile project.So i am also using Pocket PC 2002 with SQL CE 2.0.When...
7
by: ashjas | last post by:
Hello, I have run this logic on turboc++ 3.0 and it is working fine on it but its not running on msvs2008 c++. i am not able to assign the value like this *temp=*main,where main and temp are char...
1
by: differentsri | last post by:
THIS IS AN ASP.NET 1.1 APPLICATION IAM TRYING TO UPDATE THE FIELD BUT I AM NOT ABLE TO UPDATE IT? CAN U TELL THE REASON ? IT IS GIVING THE FOLLOWING ERROR BELOW I HAVE ALSO GIVEN THE CODE OF...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.