473,396 Members | 2,147 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,396 software developers and data experts.

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

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

Jun 7 '06 #1
3 3081
mi*****@gmail.com wrote:
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
Jun 7 '06 #2
On 2006-06-07, mi*****@gmail.com <mi*****@gmail.com> wrote:
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

--
Grant Edwards grante Yow! I selected E5... but
at I didn't hear "Sam the Sham
visi.com and the Pharoahs"!
Jun 7 '06 #3
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_stdou t')
os.dup2(so.fileno(), o.fileno())
os.dup2(si.fileno(), i.fileno())

Thanks for your help,
Mike

Grant Edwards wrote:
On 2006-06-07, mi*****@gmail.com <mi*****@gmail.com> wrote:
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

--
Grant Edwards grante Yow! I selected E5... but
at I didn't hear "Sam the Sham
visi.com and the Pharoahs"!


Jun 7 '06 #4

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

Similar topics

0
by: newgene | last post by:
Hi, group, I guess it is a simple question for you, but it puzzles me for a while. I use a wxTextCtrl in my application for output result and any exception msg. I know I can add new msg onto...
6
by: Tsai Li Ming | last post by:
Dear all, I have a problem with a redirecting stdout and stderr. I am a top level module and has no control over the imported modules that are making system calls such as os.system or popen2.* ....
1
by: Jacek Popławski | last post by:
Popen from subprocess module gives me access to stdout, so I can read it. Problem is, that I don't know how much data is available... How can I read it without blocking my program? example:...
2
by: Thomas W. Brown | last post by:
I have setup Console redirection within my Console app (via Console.SetOut and Console.SetErrror) to route console WriteLine calls to a logfile. This works just fine with one exception... I use...
37
by: nobody | last post by:
I am writing a framework that other developers will write plug-ins for. I would like for one of the features of the framework to be to intercept all text written to stdout/stderr and prepend...
0
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 =...
0
by: danebarney | last post by:
I'm developing a Windows CE 5.0 app using C# and I need to call a child process and then capture its stdout into a window. The solutions I've read online for doing this involve either using the...
10
by: Jef Driesen | last post by:
I wrote a program that writes a large amount of information to stdout (and stderr). When run from the commandline, this output either appears on the console window (the default) or can be...
2
by: RossGK | last post by:
Just getting used to the PyDev environment in eclipse by playing with a few simple programs. I'm also using wxPython GUI stuff. I've noticed though that simple print commands in my code cause a...
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
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.