Re: Close stdout socket on CGI after fork with subprocess

Discussion in 'Python' started by Kushal Kumaran, Jan 10, 2011.

  1. On Mon, Jan 10, 2011 at 1:15 PM, Thibaud Roussillat
    <> wrote:
    > On Sat, Jan 8, 2011 at 3:19 AM, Kushal Kumaran
    > <> wrote:
    >>
    >> On Fri, Jan 7, 2011 at 8:08 PM, Thibaud Roussillat
    >> <> 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
    Kushal Kumaran, Jan 10, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Helene Unterwieser
    Replies:
    14
    Views:
    3,647
    Esmond Pitt
    Jul 3, 2007
  2. Rafael Giannetti Viotti

    Subprocess and pipe-fork-exec primitive

    Rafael Giannetti Viotti, Jul 30, 2007, in forum: Python
    Replies:
    2
    Views:
    3,000
    Rafael V.
    Aug 1, 2007
  3. Eric Snow

    os.fork and pty.fork

    Eric Snow, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    563
    Eric Snow
    Jan 8, 2009
  4. Iñaki Baz Castillo
    Replies:
    7
    Views:
    822
    Iñaki Baz Castillo
    Jan 12, 2010
  5. kj
    Replies:
    11
    Views:
    204
    Brian McCauley
    Sep 8, 2007
Loading...

Share This Page