Opened 13 years ago

Last modified 6 years ago

#4073 enhancement new

twistd daemonization support for windows

Reported by: zseil Owned by:
Priority: normal Milestone:
Component: core Keywords: win32 twistd
Cc: vladimir@… Branch:


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 to more closely match the one in, 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 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


pid = int(open(pidfile, "rb").read())
win32console.GenerateConsoleCtrlEvent(CTRL_C_EVENT, pid)

Attachments (1)

windows-daemon.diff (6.2 KB) - added by zseil 13 years ago.
incomplete patch

Download all attachments as: .zip

Change History (5)

Changed 13 years ago by zseil

Attachment: windows-daemon.diff added

incomplete patch

comment:1 Changed 12 years ago by Glyph

Owner: changed from Glyph to PenguinOfDoom

comment:2 Changed 12 years ago by Jean-Paul Calderone

Keywords: win32 twistd added; windows removed

comment:3 Changed 11 years ago by <automation>

Owner: PenguinOfDoom deleted

comment:4 Changed 6 years ago by Vladimir Rutsky

Cc: vladimir@… added
Note: See TracTickets for help on using tickets.