How do I automatically redirect stdout and stderr when using os.popen2?

M

mikem76

How do I automatically redirect stdout and stderr when using os.popen2
to start a long running process. If the process prints a lot of stuff
to stdout it will eventually stop because it runs out of buffer space.
Once I start reading the stdout file returned by os.popen2 then the
process resumes. I know that I could just specify > /dev/null when
starting the process but I'd like to know if there is a way to start a
process using os.popen2 or some other way so that all standard out and
standard error goes to /dev/null or some other file.

Thanks,
Mike
 
D

Dennis Benzinger

How do I automatically redirect stdout and stderr when using os.popen2
to start a long running process. If the process prints a lot of stuff
to stdout it will eventually stop because it runs out of buffer space.
Once I start reading the stdout file returned by os.popen2 then the
process resumes. I know that I could just specify > /dev/null when
starting the process but I'd like to know if there is a way to start a
process using os.popen2 or some other way so that all standard out and
standard error goes to /dev/null or some other file.

Thanks,
Mike


Use the POpen class <http://docs.python.org/lib/node235.html> from the
subprocess module.


Bye,
Dennis
 
G

Grant Edwards

How do I automatically redirect stdout and stderr when using os.popen2
to start a long running process.

popen2 does redirect stdout to a file object. That's the whole
point of using it. If you don't want a file object that's
connected to the child's stdout, then don't use popen2.
If the process prints a lot of stuff to stdout it will
eventually stop because it runs out of buffer space. Once I
start reading the stdout file returned by os.popen2 then the
process resumes. I know that I could just specify > /dev/null
when starting the process but I'd like to know if there is a
way to start a process using os.popen2 or some other way so
that all standard out and standard error goes to /dev/null or
some other file.

Yes. Fork a child process then use the standard file
descriptor operators to open /dev/null or some other file and
then dup those file descriptors to stdout/stderr:

http://docs.python.org/lib/os-fd-ops.html

Then you can exec the program you want to run:

http://docs.python.org/lib/os-process.html

This is basically identical to the method use to do I/O
redirection in C, so any decent book on C programming under
Unix should have a good explanation.


Hmmm, it looks like it's simpler touse the subprocess module.
Just open /dev/null to get a file descriptor for it, and then
pass that to subprocess.Popen():

http://docs.python.org/lib/module-subprocess.html
 
M

mikem76

Here is what I ended up doing:

si = file('/dev/null', 'r')
so = file('/dev/null', 'a+')
i,o = os.popen2('some_command_that_prints_a_lot_to_stdout')
os.dup2(so.fileno(), o.fileno())
os.dup2(si.fileno(), i.fileno())

Thanks for your help,
Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top