Close stdout socket on CGI after fork with subprocess

Thibaud Roussillat thibaud.roussillat at gmail.com
Mon Jan 10 09:53:18 EST 2011


On Mon, Jan 10, 2011 at 9:26 AM, Kushal Kumaran <
kushal.kumaran+python at gmail.com <kushal.kumaran%2Bpython at gmail.com>> wrote:

> On Mon, Jan 10, 2011 at 1:15 PM, Thibaud Roussillat
> <thibaud.roussillat at gmail.com> wrote:
> > On Sat, Jan 8, 2011 at 3:19 AM, Kushal Kumaran
> > <kushal.kumaran+python at gmail.com <kushal.kumaran%2Bpython at gmail.com>>
> wrote:
> >>
> >> On Fri, Jan 7, 2011 at 8:08 PM, Thibaud Roussillat
> >> <thibaud.roussillat at gmail.com> wrote:
> >> > Hi,
> >> >
> >> > I work with Python 2.4 and CGI.
> >> >
> >> > I have a CGI which call a Python script in background process and
> return
> >> > result before background task is finished.
> >> >
> >> > Actually, the browser displays response but it is waiting for end of
> >> > background task because the socket is not closed.
> >> >
> >> > Internet told me that I must close the stdout file descriptor
> >> > (sys.stdout.close()) to close the socket but it doesn't work.
> >> >
> >> > The background task is launched via subprocess.Popen and is attached
> to
> >> > the
> >> > root process on ps command.
> >> >
> >>
> >> This means that the parent process finished before the child.  Call
> >> wait() on the Popen object to wait for the child to terminate.
> >> Depending on how you create the Popen object, the child process may
> >> inherit your own stdout.  In that case, the child process may be
> >> keeping the socket open after the parent dies.
> >>
> >
> > <top-posting corrected>
> >
> > In fact, the parent process finished before the child, it's why I want to
> > run the child in a forked process, and close the socket of the parent
> task.
> >
> > The goal is not to wait for the child process but to leave it lead one's
> own
> > life as a background task. The client don't have to wait for the end of
> the
> > child process.
> >
> > Is there a way to not inherit from the parent stdout on the child process
> ?
> >
>
> open os.devnull and pass that file object as stdin, stdout and stderr
> for the child process.  Hopefully the program you are running has been
> designed not to expect to be able to use stdin/stdout/stderr.
>
> Please keep the discussion on the mailing list.  Other people on the
> list are smarter than me.  Also, the convention on this mailing list
> is to keep replies below the quoted content.
>
> --
> regards,
> kushal
>

Thanks a lot, this works well with a file object opened on /dev/null (or
os.devnull) and passed as stdin, stdout and stderr.

Sorry for the reply, I just do "reply" on my webmail ;)

Regards,

Thib
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110110/c32b367a/attachment.html>


More information about the Python-list mailing list