Redirect stdout & stderr (similar to a daemon)

Donn Cave donn at drizzle.com
Sun May 30 22:40:38 EDT 2004


Quoth Tsai Li Ming <mailinglist at ltsai.com>:
| Donn Cave wrote:

| > There is, if I understand what you meant by that.  Consider
| > 
| >    old0 = os.dup(0)
| >    os.dup2(si.fileno(), 0)
| > 
| > The original input stream is held open on some arbitrary
| > unit, which you may use at any point to restore it to standard
| > input.
| > 
| >    os.dup2(old0, 0)
| >    os.close(old0)
| > 
| > 	Donn Cave, donn at drizzle.com
|
| Thanks Donn,
|
| Why is there a need to close old0? Because of the extra file handle?

Good question.  It's just barely conceivable that it could cause a
problem - say, where a child process redirects 0 but doesn't account
for the dup'd version, the parent exits, the file stays open when
it should have closed on exit, and something else has been depending
on that.  To start with, most normal ways to fork a child process
will close all the extra files anyway, so this is highly improbable.

I would still do it, but it's more style than function.

	Donn Cave, donn at drizzle.com



More information about the Python-list mailing list