Close stdout socket on CGI after fork with subprocess

Kushal Kumaran kushal.kumaran+python at gmail.com
Fri Jan 7 21:19:18 EST 2011


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.

-- 
regards,
kushal



More information about the Python-list mailing list