twistd daemonization support for windows
[email protected] Sun, 08 Nov 2009 15:43:54 -0000
| Newsgroups | gmane.comp.python.twisted.bugs |
|---|---|
| Message-ID | <[email protected]> |
New submission from zseil <[email protected]>: twistd currently doesn't support daemonization on Windows. There are plans to provide similar functionality via NT services, but that will require changes to the reactors (an NT service program has to respond to service events and report its status to the service control manager, which means that the current `_SignalReactorMixin` base class would have to be replaced with one that used `RegisterServiceCtrlHandler` instead of `signal.signal`). In the mean time, here is an incomplete patch that provides UNIX-like daemonization functionality on Windows. The main thing it lacks are tests and documentation. I only manually tested the patch and only under Windows XP running as a privileged user. The patch refactors the code in `_twistdw.py` to more closely match the one in `_twistd_unix.py`, to help with code duplication removal in the future. The patch also contains the `getPythonArgs` function, which might be useful for `_twistd_unix.launchWithName` too, and a fix for #2572. I don't think I'll be able to work on this further in the near future, but it is probably better for a patch to rot on the bug tracker than on my disk :) If somebody finds this useful, please, finish it and try to get it in the trunk. Here are some implementation notes: * We have to spawn two child processes, similar to `_twistd_unix.daemonize`. Due to lack of `fork`, this means that we have to use a nasty `if __name__ == "__main__"` hack. The hack would break if `_twistw.py` functionality was moved to the `twistd` script itself. * The first child is spawned to detach from the console and job object and to close all inherited handles (via `bInheritHandles = False` parameter to `CreateProcess`). * This child then has to open null `std*` files and prepare a new desktop for the second child, otherwise the processes from the starting desktop would be able to directly send messages to the daemon process. * The second child has to be created as a process group leader with a hidden console. The console is necessary because signals on Windows are implemented as console ctrl events. * It would be nice if at least the console detaching functionality was available even when `pywin32` package is not installed, by issuing a single `os.spawn*` call with `os.P_DETACH` as the `mode` parameter. * Using the pid of the first child for desktop name is not optimal, because that child exits immediately and its pid might get reused. * Once #2646 is implemented, it would be possible to gracefully shutdown daemonized processes with something like this: {{{ import win32console CTRL_C_EVENT = 0 CTRL_BREAK_EVENT = 1 pid = int(open(pidfile, "rb").read()) win32console.FreeConsole() win32console.AttachConsole(pid) win32console.GenerateConsoleCtrlEvent(CTRL_C_EVENT, pid) }}} ---------- Type : enhancement Component: core Keywords : windows Priority : normal Nosy : ---------- http://twistedmatrix.com/trac/ticket/4073