Launching a subprocess without waiting around for the result?

E

erikcw

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!
 
E

erikcw

For "how do I deal with subprocesses from Python", the (new in Python
2.4) 'subprocess' module is the default go-to answer


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?
 
R

r0g

erikcw said:
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.
 
R

r0g

erikcw said:
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
 
G

Gary Herron

Almar said:
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
 
M

Michael Palmer

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top