twistd daemonization support for windows
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 6k
- Forks
- 1.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 10
Description
| @zseil reported | |
|---|---|
| Trac ID | trac#4073 |
| Type | enhancement |
| Created | 2009-11-08 15:43:54Z |
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 offork, this means that we have to use a nastyif __name__ == "__main__"hack. The hack would break if_twistw.pyfunctionality was moved to thetwistdscript itself. - The first child is spawned to detach from the console and job object and to close all inherited handles (via
bInheritHandles = Falseparameter toCreateProcess). - 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
pywin32package is not installed, by issuing a singleos.spawn*call withos.P_DETACHas themodeparameter. - 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)
Attachments:
- windows-daemon.diff (6355 bytes) - added by zseil on 2009-11-08 15:44:53Z - incomplete patch
Searchable metadata
trac-id__4073 4073
type__enhancement enhancement
reporter__zseil zseil
priority__normal normal
milestone__
branch__
branch_author__
status__new new
resolution__None None
component__core core
keywords__win32_twistd win32 twistd
time__1257695034000000 1257695034000000
changetime__1462676869127349 1462676869127349
version__None None
owner__
cc__vladimir@...
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the incomplete windows-daemon.diff and the existing _twistdw.py and _twistd_unix.py implementations. Reproduce the manually tested Windows behavior, then determine the required daemonization, test, and documentation coverage from the implementation notes. Done means supported Windows daemonization is tested and documented, without leaving the patch's known gaps unresolved.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- cli, operating-systems
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100