[Tutor] cgi script to start another process in background

Jay Deiman jay at splitstreams.com
Thu Feb 26 18:39:26 CET 2009


Ravi Kondamuru wrote:
> Hi,
> 
> I am trying to write a python cgi script, that invokes another process 
> and exists.
> Using the subprocess documentation on NO_WAIT, I am not having much success:
> 
> pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg")
> ==>
> pid = Popen(["/bin/mycmd", "myarg"]).pid
> 
> The script seems to wait for the new process to exit before returning to 
> the user.
> 
> I tried doing the double-fork approach discussed here:
> http://code.activestate.com/recipes/66012/

Are you on some kind of Unix box here?  The fork approach should
definitely work for you.  An even simpler example, which should still
work for you, is here:

========================
import os , time , sys

print 'hello before the fork'

if os.fork():
     print 'parent exiting'
     sys.exit()

print 'hello from child: %d' % os.getpid()
time.sleep(30)
os._exit(0)
========================

You should be able to verify that you have that process still running
after the main process exits.

-- 
Jay Deiman

\033:wq!



More information about the Tutor mailing list