[python-win32] Q: daemon spawn from within apache cgi blocks parent process

replytodirk@web.de replytodirk at web.de
Tue May 24 23:48:30 CEST 2005


> On Tue, 24 May 2005 00:43:32 +0200, <replytodirk at web.de> wrote:
>
> >I want to create a daemon process from within a python cgi script running
on
> >apache/win32. This daemon's lifetime is longer than that of the creating
cgi
> >script - of course, because the script starts the daemon, writes some
html
> >code to stdout and then exits. This simple task kept me busy for weeks!
> >...
> >Does anybody have an idea what goes wrong here? Is there any connection
> >between a parent and a child process which has to be released before the
> >processes can die independently? Are there any Apache-specific things to
> >handle when spawning processes?

Thanks to Robin Becker and Tim Roberts this problem is solved!

CreateProcess(..., InheritHandles=0, creationflags=DETACHED_PROCESS) does
the trick. I could not get the other idea regarding closing stdout in the
daemon to work as well, though. I tried the following:

In the daemon-creator:
    Child = Popen(CommandLine, stdin=PIPE, stdout=PIPE, stderrPIPE,
creationflags=DETACHED_PROCESS)
    Child.stderr.close()
    Child.stdout.close()
    Child.stdin.close()

And in the daemon:
    os.close(0)
    os.close(1)
    os.close(2)

But it didn't work. The creator still was somehow connected to the daemon,
because it didn't terminate until the daemon itself terminated. It's not
that much of a problem because I use the InhertiHandles=0 method, but still
strange and not understood...

Thanks again guys,
Dirk



More information about the Python-win32 mailing list