Launching a subprocess without waiting around for the result?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • erikcw

    Launching a subprocess without waiting around for the result?

    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!
  • Ben Finney

    #2
    Re: Launching a subprocess without waiting around for the result?

    erikcw <erikwickstrom@ gmail.comwrites :
    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.Pop en' 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

    Comment

    • erikcw

      #3
      Re: Launching a subprocess without waiting around for the result?

      On Sep 18, 3:33 pm, Ben Finney <bignose+hate s-s...@benfinney. id.au>
      wrote:
      erikcw <erikwickst...@ gmail.comwrites :
      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.Pop en' 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?

      Comment

      • Ben Finney

        #4
        Re: Launching a subprocess without waiting around for the result?

        erikcw <erikwickstrom@ gmail.comwrites :
        On Sep 18, 3:33 pm, Ben Finney <bignose+hate s-s...@benfinney. id.au>
        wrote:
        erikcw <erikwickst...@ gmail.comwrites :
        What is the correct way to launch subprocess without waiting for
        the result to return?
        Creating an instance of 'subprocess.Pop en' will launch the process
        and return the Popen instance. You then have the option of polling
        it or waiting for it to complete.
        >
        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?
        Ah, no, that's a different thing. If the parent exits, the child will
        also be killed I believe.

        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.

        --
        \ “Pinky, are you pondering what I'm pondering?” “Yes Brain, but |
        `\ if our knees bent the other way, how would we ride a bicycle?” |
        _o__) —_Pinky and The Brain_ |
        Ben Finney

        Comment

        • John Fabiani

          #5
          Re: Launching a subprocess without waiting around for the result?

          erikcw wrote:
          On Sep 18, 3:33 pm, Ben Finney <bignose+hate s-s...@benfinney. id.au>
          wrote:
          >erikcw <erikwickst...@ gmail.comwrites :
          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.Pop en' 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?
          Just so happens that I ran into the same problem recently. I started with
          exec(), then os.system(), next os.popen(), and last os.spawn().
          This is what I discovered on a windows platform. The exec() replaced the
          current running process. os.system did not start a completely new process.
          os.popen() created a new process but did not open a command box to display
          any print statements. Lastly os.spawn() worked - it created a new process
          and opened a command box to display any print statements I needed.

          Johnf

          Comment

          • r0g

            #6
            Re: Launching a subprocess without waiting around for the result?

            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.

            Comment

            • r0g

              #7
              Re: Launching a subprocess without waiting around for the result?

              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(yourc ommandline+" &")

              ;-)

              Roger

              Comment

              • Marco Bizzarri

                #8
                Re: Launching a subprocess without waiting around for the result?

                On Fri, Sep 19, 2008 at 10:48 PM, Almar Klein <almar.klein@gm ail.comwrote:
                >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
                >
                --

                >

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



                (not tested)

                Regards
                Marco
                --
                Marco Bizzarri
                Where we talk about coding, Dungeons and Dragons, and stuff.


                Comment

                • Gary Herron

                  #9
                  Re: Launching a subprocess without waiting around for the result?

                  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

                  Comment

                  • Michael Palmer

                    #10
                    Re: Launching a subprocess without waiting around for the result?

                    On Sep 18, 5:33 pm, erikcw <erikwickst...@ gmail.comwrote:
                    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


                    Comment

                    Working...