Launching a subprocess without waiting around for the result?

Discussion in 'Python' started by erikcw, Sep 18, 2008.

  1. erikcw

    erikcw Guest

    Hi,

    I have a cgi script where users are uploading large files for
    processing. I want to launch a subprocess to process the file so the
    user doesn't have to wait for the page to load.

    What is the correct way to launch subprocess without waiting for the
    result to return?

    Thanks!
     
    erikcw, Sep 18, 2008
    #1
    1. Advertising

  2. erikcw

    erikcw Guest

    On Sep 18, 3:33 pm, Ben Finney <>
    wrote:
    > erikcw <> writes:
    > > I have a cgi script where users are uploading large files for
    > > processing. I want to launch a subprocess to process the file so the
    > > user doesn't have to wait for the page to load.

    >
    > For "how do I deal with subprocesses from Python", the (new in Python
    > 2.4) 'subprocess' module is the default go-to answer
    > <URL:http://www.python.org/doc/lib/module-subprocess>, replacing a
    > rather fragmented set of modules before it.
    >
    > > What is the correct way to launch subprocess without waiting for the
    > > result to return?

    >
    > Creating an instance of 'subprocess.Popen' will launch the process and
    > return the Popen instance. You then have the option of polling it or
    > waiting for it to complete.
    >
    > --
    >  \     “To stay young requires unceasing cultivation of the ability to |
    >   `\                   unlearn old falsehoods.” —Robert Anson Heinlein |
    > _o__)                                                                  |
    > Ben Finney


    So if I create a Popen object and then just ignore the object and exit
    the program the subproccess will finish it's work and then exit itself
    cleanly?
     
    erikcw, Sep 19, 2008
    #2
    1. Advertising

  3. erikcw

    r0g Guest

    erikcw wrote:
    > Hi,
    >
    > I have a cgi script where users are uploading large files for
    > processing. I want to launch a subprocess to process the file so the
    > user doesn't have to wait for the page to load.
    >
    > What is the correct way to launch subprocess without waiting for the
    > result to return?
    >
    > Thanks!


    Try exec() with " &" at the end of your command line.

    Roger.
     
    r0g, Sep 19, 2008
    #3
  4. erikcw

    r0g Guest

    erikcw wrote:
    > Hi,
    >
    > I have a cgi script where users are uploading large files for
    > processing. I want to launch a subprocess to process the file so the
    > user doesn't have to wait for the page to load.
    >
    > What is the correct way to launch subprocess without waiting for the
    > result to return?
    >
    > Thanks!


    Whoops, that was PHP! Imeant...

    os.system(yourcommandline+" &")

    ;-)

    Roger
     
    r0g, Sep 19, 2008
    #4
  5. On Fri, Sep 19, 2008 at 10:48 PM, Almar Klein <> wrote:
    >> Ah, no, that's a different thing. If the parent exits, the child will
    >> also be killed I believe.

    >
    > Not if it's stuck in some endless loop...
    >
    >> If you want to spawn a process and have it live on independent of the
    >> parent, you want to make the child process a "daemon", detatching
    >> itself from the parent's environment. I don't recall how that's done
    >> immediately, but those are the terms to search for.

    >
    > I'm curious how this can be done, does anyone know this?
    >
    > Almar
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >



    First result in "making a daemon in python with google":

    http://mail.python.org/pipermail/python-list/2007-February/427692.html

    (not tested)

    Regards
    Marco
    --
    Marco Bizzarri
    http://notenotturne.blogspot.com/
    http://iliveinpisa.blogspot.com/
     
    Marco Bizzarri, Sep 19, 2008
    #5
  6. erikcw

    Gary Herron Guest

    Almar Klein wrote:
    >
    > Ah, no, that's a different thing. If the parent exits, the child will
    > also be killed I believe.
    >
    >
    > Not if it's stuck in some endless loop...
    >
    > If you want to spawn a process and have it live on independent of the
    > parent, you want to make the child process a "daemon", detatching
    > itself from the parent's environment. I don't recall how that's done
    > immediately, but those are the terms to search for.
    >
    >
    > I'm curious how this can be done, does anyone know this?


    I just dove into this several day ago for a small project.

    On Linux it's easy -- it involves a couple of forks and other system
    calls. Google for daemonize.py.
    <http://github.com/lfittl/python-helpers/tree/master/daemonize.py>

    On Windows, a bit of searching seems to find a consensus that the way to
    do something similar is as a Window's service. I'm just now looking
    into how to register and start a service, and how to stop and remove it
    later. Google finds lots of information on this -- perhaps I'll post my
    result when I've pulled it all together.

    Gary Herron
     
    Gary Herron, Sep 19, 2008
    #6
  7. On Sep 18, 5:33 pm, erikcw <> wrote:
    > Hi,
    >
    > I have a cgi script where users are uploading large files for
    > processing. I want to launch a subprocess to process the file so the
    > user doesn't have to wait for the page to load.
    >
    > What is the correct way to launch subprocess without waiting for the
    > result to return?
    >
    > Thanks!


    both os.spawn or subprocess can be used. I actually find subprocess
    hard to remember so usually prefer os.spawn. For various examples and
    explanations, see

    http://effbot.org/librarybook/os.htm
     
    Michael Palmer, Sep 19, 2008
    #7
    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. Miles Kaufmann

    Re: Waiting for a subprocess to exit

    Miles Kaufmann, Aug 21, 2009, in forum: Python
    Replies:
    6
    Views:
    324
  2. Michael Tan
    Replies:
    32
    Views:
    1,052
    Ara.T.Howard
    Jul 21, 2005
  3. Robert Oschler
    Replies:
    2
    Views:
    100
    Chris Riesbeck
    Sep 17, 2003
  4. Replies:
    17
    Views:
    410
    Terry Reedy
    Sep 19, 2013
  5. Jean-Michel Pichavant

    Re: subprocess call is not waiting.

    Jean-Michel Pichavant, Sep 13, 2012, in forum: Python
    Replies:
    0
    Views:
    206
    Jean-Michel Pichavant
    Sep 13, 2012
Loading...

Share This Page