I am running this on a freebsd 4.9 system running apache server. I started
with code that was as simple as yours. But the problem is apache does not
print "hello before the fork" until the child exists.I have read at various
forums that apache buffers till the script exists and in this case till the
child exists and then the parent itself. So "hello before the fork" is
printed after 30 secs on the browser. This approach I believe works when
executed from the shell but not in the apache context.
I finally got the script working after using the following recipe to make
the process a daemon.
http://code.activestate.com/recipes/278731/

In place of os.fork() in the script I called createDaemon mention from the
module in the recipe and that did the trick. I had to convert the os._exit()
to sys.exit() in the createDaemon function otherwise the was giving
"Premature end of script headers" error.

thanks,
Ravi.

On Thu, Feb 26, 2009 at 9:09 AM, Jay Deiman <j...@splitstreams.com> wrote:

> 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!
>
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to