I'm calling a command from within a python script and I need to be
able to both catch the output (stdout and stderr) from it and also
have the PID (so that I can kill it)
I can do one or other of these, but I can't find any way to do them
both at the same time.
So far, I've got the following options:
To get the output from the command: fdin, fdout, fderr = os.popen3(command)
Or to get the PID so that I can kill it:
pid = os.spawnvp(os.P_NOWAIT, command.split()[0], command.split())
I've tried doing the following to grab stderr (I only need stderr, not
stdout in this case)
fderr = os.pipe() sys.stderr = fderr pid = os.spawnvp(os.P_NOWAIT, command.split()[0], command.split())
But this doesn't seem to help.... It still just outputs stderr to the
terminal and when I try to do fderr.read(1) I don't get anything...
Any help would be very welcome
Hugh Macdonald 8 3583 hu**@brokenpipefilms.com (Hugh Macdonald) wrote in message news:<3c**************************@posting.google. com>... I'm calling a command from within a python script and I need to be able to both catch the output (stdout and stderr) from it and also have the PID (so that I can kill it)
Take a look in PyPi for popen5. It may do what you want, but only in
Linux: the Windows version isn't complete yet.
S
----- Original Message -----
From: "Stewart Midwinter" <st*****@midwinter.ca>
Subject: Re: Getting both PID and output from a command hu**@brokenpipefilms.com (Hugh Macdonald) wrote in message
news:<3c**************************@posting.google. com>... I'm calling a command from within a python script and I need to be able to both catch the output (stdout and stderr) from it and also have the PID (so that I can kill it)
Take a look in PyPi for popen5. It may do what you want, but only in Linux: the Windows version isn't complete yet.
I'll take a look - thanks....
Hugh Macdonald
In article <3c**************************@posting.google.com >, hu**@brokenpipefilms.com (Hugh Macdonald) wrote: I'm calling a command from within a python script and I need to be able to both catch the output (stdout and stderr) from it and also have the PID (so that I can kill it)
I can do one or other of these, but I can't find any way to do them both at the same time.
So far, I've got the following options:
To get the output from the command:
fdin, fdout, fderr = os.popen3(command)
You're close there. You're creating a class instance
that has the pid, but os.popen3 hides it from you.
See popen3.py in the library for details.
Donn Cave, do**@u.washington.edu hu**@brokenpipefilms.com (Hugh Macdonald) wrote in message news:<3c**************************@posting.google. com>... I'm calling a command from within a python script and I need to be able to both catch the output (stdout and stderr) from it and also have the PID (so that I can kill it)
I can do one or other of these, but I can't find any way to do them both at the same time.
So far, I've got the following options:
To get the output from the command:
fdin, fdout, fderr = os.popen3(command)
Or to get the PID so that I can kill it: pid = os.spawnvp(os.P_NOWAIT, command.split()[0], command.split())
I've tried doing the following to grab stderr (I only need stderr, not stdout in this case) fderr = os.pipe() sys.stderr = fderr pid = os.spawnvp(os.P_NOWAIT, command.split()[0], command.split())
But this doesn't seem to help.... It still just outputs stderr to the terminal and when I try to do fderr.read(1) I don't get anything...
Any help would be very welcome
Hugh Macdonald
Hi,
The Pexpect library lets you run external commands.
You get output from a command and you can get the pid. http://pexpect.sourceforge.net/
You should be able to write code like this:
import pexpect
child = pexpect.spawn (command.split()[0], command.split())
print child.pid
try:
print child.read()
except pexpect.TIMEOUT:
child.kill (9)
One problem is that the stdout and stderr are merged into a single stream.
This is a limitation of the Python pty library. Oops...
You will still be able to read the error, but you can't read it
separately from stdout.
Note, I wouldn't trust using a pipe. You will not see any data on the pipe
until the child decides to flush the pipe. There is no way to force the
child to flush it's stdout (your stdin from your point of view).
Pipes are bad for working with child apps that use the stdio libary.
You need a pty for that.
Yours,
Noah
I suspect I'll probably use Donn Cave's suggestion when I give it a go on
monday - I'd rather not use any external modules if I can help it, and I
know that stderr gives me output in a format that I can read (I know exactly
which command I want to run here, and I have the program working properly
except for not being able to stop if before it finishes on its own.....)
Hugh Macdonald
----- Original Message -----
From: "Noah" <no**@noah.org>
Subject: Re: Getting both PID and output from a command The Pexpect library lets you run external commands. You get output from a command and you can get the pid. http://pexpect.sourceforge.net/ You should be able to write code like this:
import pexpect child = pexpect.spawn (command.split()[0], command.split()) print child.pid try: print child.read() except pexpect.TIMEOUT: child.kill (9)
One problem is that the stdout and stderr are merged into a single stream. This is a limitation of the Python pty library. Oops... You will still be able to read the error, but you can't read it separately from stdout.
Note, I wouldn't trust using a pipe. You will not see any data on the pipe until the child decides to flush the pipe. There is no way to force the child to flush it's stdout (your stdin from your point of view). Pipes are bad for working with child apps that use the stdio libary. You need a pty for that.
Yours, Noah -- http://mail.python.org/mailman/listinfo/python-list
Hugh Macdonald <hu**@brokenpipefilms.com> wrote: I'm calling a command from within a python script and I need to be able to both catch the output (stdout and stderr) from it and also have the PID (so that I can kill it)
I can do one or other of these, but I can't find any way to do them both at the same time.
So far, I've got the following options:
To get the output from the command:
fdin, fdout, fderr = os.popen3(command)
To get stdout and stderr as separate pipes and also have the child's
PID, you want to use the Popen3 class from the popen2 module. See: http://www.python.org/doc/current/li...le-popen2.html http://www.python.org/doc/current/li...3-objects.html
for details.
--
Robin Munn rm***@pobox.com
In article <ma************************************@python.org >,
Hugh Macdonald <Hu***********@brokenpipefilms.com> wrote: I suspect I'll probably use Donn Cave's suggestion when I give it a go on monday - I'd rather not use any external modules if I can help it, and I know that stderr gives me output in a format that I can read (I know exactly which command I want to run here, and I have the program working properly except for not being able to stop if before it finishes on its own.....)
Fair enough - thanks for the suggestion....
I have actually already gone the other way, which works perfectly, thanks.
If I find problems with the method I'm using, then I will certainly take a
look at this one...
Hugh Macdonald
----- Original Message -----
From: "Cameron Laird" <cl****@lairds.com>
To: <Hu***********@brokenpipefilms.com>
Sent: Tuesday, March 30, 2004 2:14 AM
Subject: Re: Getting both PID and output from a command In article <ma************************************@python.org >, Hugh Macdonald <Hu***********@brokenpipefilms.com> wrote:I suspect I'll probably use Donn Cave's suggestion when I give it a go on monday - I'd rather not use any external modules if I can help it, and I know that stderr gives me output in a format that I can read (I know
exactlywhich command I want to run here, and I have the program working properly except for not being able to stop if before it finishes on its own.....) . . . Please understand that, as external modules go, Pexpect is easy to like, because it's "pure Python". It's just some Python source code. You can choose to put it all inside *your* program, then there's nothing "external". If you wish, just think of it as a particularly long Usenet reply that has already been tested. --
Cameron Laird <cl****@phaseit.net> Business: http://www.Phaseit.net
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Sami Viitanen |
last post by:
Hello,
I'm using os.popen and read for reading command input to string but the
string doesn't contain the same output that running the command manually or
with os.system contains.
with...
|
by: Chris Maloof |
last post by:
Hello,
Does anyone know how I can read the ASCII text from a console window
(from another application) in WinXP? It doesn't sound like a major
operation, but although I can find the window via...
|
by: gregpinero |
last post by:
Hey everyone,
I'm trying to call a system command "svnlook log \arms" from within
python and process the results. However I'm having trouble getting the
results of the command back into python....
|
by: Christian Convey |
last post by:
Hello,
I've got a program that (ideally) perpetually monitors sys.stdin for
lines of text. As soon as a line comes in, my program takes some
action.
The problem is, it seems like a very large...
|
by: ruju00 |
last post by:
I am getting an error in Login() method of the following class
FtpConnection
public class FtpConnection
{
public class FtpException : Exception
{
public FtpException(string message) :...
|
by: SF |
last post by:
Hello All,
In a windows C learning project I am wokring on I use the system
function to run a command, I want to suck the results into a vairable.
The system function seems to only return an...
|
by: noLoveLusT |
last post by:
hi everyone i am very very new to the sql server (2 days actually and ) so
far i learned creating SPs etc but couldnt workout how to get return value
from my prodecure
my sp as follows...
|
by: leescriven |
last post by:
Hi,
Thi is my first post on this forum, and I'm hoping that there is a guru out there who can help me with an annoying problem I am having.
I have written a Stored Procedure that relies heavily...
|
by: Mike Lester |
last post by:
I have a need for a stored procedure to return a recordset AND an output parameter that contains the count of records in the recordset.
I can get either but not both. (ie. if there is a select...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
header("Location:".$urlback);
Is this the right layout the...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
| |