From: clarence at silcom.com (Clarence Gardner)
Date: Mon, 12 Apr 1999 07:03:13 GMT
Subject: forking + stdout = confusion
Message-ID: <RUgQ2.31$Oq4.32657@newsfeed.avtel.net>
Content-Length: 1947
X-UID: 828                                                  

I have a program running in Python 1.5.1 under FreeBSD 2.1.7.
It happens to be a nph-type CGI program.  Normally, it runs a
report that can take quite a while, and I'm trying to add an
option to delay the start.  I defined this function:

def LateShift():
    OutputID = `os.getpid()`
    Child = os.fork()
    if Child:
        print 'content-type: text/plain'
        print
        print '''The program is waiting to run later.  The output will
                be available with the id number of %s.
                ''' % OutputID
        sys.exit(0)
    sys.stdin.close()
    sys.stdout.close()
    sys.stderr.close()
    sys.stdout = open(os.path.join(SpoolDir, OutputID), 'w')
    sys.stderr = sys.stdout
    time.sleep(15)

The idea is that, after forking, one of the processes will tell the
user how to get the report output later, then exit, and the other
will wait until some appropriate time and the run the report, with
the output going to a file that normally goes to the web browser.

The problem is that the browser's connection to the server is not
being closed until the actual report is done (i.e., until the caller
of this function exits).  It seems to me that the three standard
I/O streams are closed (which they are immediately in both processes),
that should do it.  Being an nph program, there should be no connection
to the HTTP server (which is Apache), but just in case it was waiting
for the child process to exit, I had the parent (Apache's child) exit
immediately and its child do the waiting, but it works the same either
way.

I thought that maybe sys.stdout was using a dup()'ed copy of stdout,
but I checked sys.stdout.fileno() and it was 1, so that doesn't seem
to be the case.

Can anyone hazard a guess (or see right off the top of his head) what's
going on here?  Thanks for any help.

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




