forking + stdout = confusion

Clarence Gardner clarence at silcom.com
Wed Apr 14 11:11:15 EDT 1999


Roy Smith (roy at popmail.med.nyu.edu) wrote:
: Carsten Schabacker <cs at spock.rhein-neckar.de> wrote:
: > No, the child and the parent share the SAME filedescriptors.  This is true
: > for ALL open files!  So if you don't need the descriptors in a child or a
: > parent you should close them.  
: > 
: > Apache waits for the end of the file, so all processes (parents and
: > childs) must close the file!
Carsten:  Please take this constructively :)  I am aware of the fd sharing,
and the code I posted clearly closed stdin, stdout, and stderr in both the
parent and child processes.  Also, in case you're not aware of it, the
distinguishing feature of a nph-type CGI program is that its output does
not go through the web server.

: 
: When doing a similar thing a while ago, I had similar problems :-)  I
: eventually found that in addition to doing sys.stdout.close(), I had to do
: os.close(1) to make sure the fd really got closed.
: 
: I'm still not sure I understand what was going on.  I know that
: sys.__stdout__ contains a copy of sys.stdout, so at first I figured that
: doing both sys.stdout.close() and sys.__stdout__.close() would do the
: trick, but it didn't, but os.close(1) did.

That does indeed do the trick, but you probably had to os.close(2) also.
(Both stdout and stderr normally are piped into Apache.)
However, your first thought also works, with the same caveat about stderr.
stdin, stdout, and stderr all have the __xxx__ copy in the sys module
(which I was not aware of).

So it was a Python issue after all!  Removes any tinge of guilt I had
about posting it here :)


-- 
-=-=-=-=-=-=-=-=
Clarence Gardner
AvTel Communications
Software Products and Services Division
clarence at avtel.com




More information about the Python-list mailing list