Opened 17 years ago

Last modified 9 years ago

#102 enhancement assigned

Support running Twisted code as NT services on Win32.

Reported by: spiv Owned by:
Priority: normal Milestone:
Component: core Keywords: twistd
Cc: Cory Dodt, spiv, itamarst, Moshe Zadka, teratorn, Trent.Nelson Branch:


Change History (17)

comment:1 Changed 17 years ago by spiv

It could probably be done with something like a "twistd
--nt-service" option for Win32, perhaps?  Hmm, except you
need to register services.

I think a "twistd --register-nt-service" option could work,
and then you'd just let the user run it using the normal NT
service management utils.  It should probably have an
--unregister-nt-service, too.

I'd volunteer to develop this, but I don't have access to
WinNT anymore.

comment:2 Changed 17 years ago by spiv

Actually, perhaps a "tap2ntsvc" (similar to tap2deb, etc) is
a better idea than hacking this onto twistd?

comment:3 Changed 17 years ago by itamarst

hmmm. I suppose it depends exactly how NT services are
expected to work. And how would we integrate this with
py2exeish things?

comment:4 Changed 17 years ago by itamarst

We now have a twistw. This should probably go there in some

comment:5 Changed 16 years ago by slyphon

just my 2 cents, cygwin has 'cygrunsrv' which allows setup, removal, starting,
and stopping of programs (exe's, shell-scripts, whatever) as windows nt
services. this information may be of some use to whoever decides to attack this

comment:6 Changed 16 years ago by Cory Dodt

(Nosy myself.)

I second spiv's notion of tap2ntsvc. 

Services are fundamentally an install-time thing, whereas twistd is a runtime
thing.  As I see it, "twistw"'s role should just be to invoke pythonw.exe
instead of python.exe so that the process doesn't have a terminal.

If you use tap2ntsvc, you're taking the startup completely out of twistd/twistw
space and putting it into Windows space, but this is how Windows makes you do

comment:7 Changed 16 years ago by itamarst

There is no twistw as such right now - it's merely the script that
scripts/ calls on windows. There is of course a need for something that
uses pythonw.exe.

Anyway, tap2ntsvc.... that's actually probably pretty easy to write. And it
could easily have a switch for making a .exe using py2exe.

comment:8 Changed 16 years ago by radix

MoonFallen is working on this in his sandbox:


He seems to have some grand plans for it.

comment:9 Changed 15 years ago by radix

this is implemented, afaik. resolving.

comment:10 Changed 15 years ago by Cory Dodt

There's only some stuff in the sandbox with more work to do.  Re-opening, ->

See sandbox/moonfallen/TODO.txt for details.

comment:11 Changed 14 years ago by Jean-Paul Calderone

Component: core

Any chance of getting this code out of the sandbox and into the real world?

comment:12 Changed 13 years ago by teratorn

Cc: teratorn added

comment:13 Changed 12 years ago by zooko

I guess this is related to #2646 -- "twistd should write a pidfile on win32" -- and perhaps to [6678] and [17973].

comment:14 Changed 12 years ago by Trent.Nelson

Cc: Trent.Nelson added

Adding my 2c; I played around with ntsvc and twistd a while ago and it wasn't a fruitful experience. The service didn't start, or register, or something like that. Whatever the approach, it definitely should work with py2exe -- running an exe as the service rather than a batch file or .py or whatever is always going to be more desirable.

ntsvc isn't that fancy, and it's pretty easy to implement a fully functioning Win32 service with all the bells and whistles with a reasonable amount of pywin32 code.

comment:15 Changed 10 years ago by Jean-Paul Calderone

Keywords: twistd added

comment:16 Changed 9 years ago by marcuslow

i used twisted for my windows services, the best and easiest way is just to use the pythonservice.exe that comes with pythonwin. Works perfectly. In fact pythonservice.exe should and can be used for creating any python services for windows.It runs on 32bit Xp,Vista,Win 2000, 2003, windows 7, Win 2008 and 64 bit also.

Just fill up the "SvcDoRun" and "SvcStop". See a real example below. In my example smtpserver._main(xxx) starts the reactor.

#!/usr/bin/env python #coding:utf-8 # Author: marcus Low @ internetnow # Created: 2/9/2009 # Copyright (c) 2008-2009 InternetNow International Sdn Bhd # See LICENSE for details.

import win32serviceutil import win32service import win32event

import smtpserver import win32api

class PythonService(win32serviceutil.ServiceFramework):

_svc_name_ = "mnowsmtppython" _svc_display_name_ = "MailNow! 5 ESMTP" def init(self, args):

win32serviceutil.ServiceFramework.init(self, args) # Create an event which we will use to wait on. # The "service stop" request will set this event. self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) self.stop = False

def SvcStop(self):

# Before we do anything, tell the SCM we are starting the stop process. self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) self.stop = True smtpserver.reactor.stop() # And set my event. win32event.SetEvent(self.hWaitStop) win32api.Sleep(1000)

def SvcDoRun(self):

if smtpserver._main(None, None, service=True) :

win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)

if name=='main':


comment:17 Changed 9 years ago by <automation>

Owner: Cory Dodt deleted
Note: See TracTickets for help on using tickets.