[Twisted-Python] More about twistd and win32 service

Justin Johnson justinjohnson at fastmail.fm
Fri Oct 31 09:31:30 EST 2003

Itamar and others,

Sorry for the delayed reply and thank you for your constant help.  I
setup my NT service as you described, importing the win32eventreactor and
installing it.  The service starts up fine, but when I stop the service I
get the following in the logs.  The "15:08 ... Unexpected..." entry only
gets dumped in the log when stopping the service.

I'm not sure how I'd go about debugging this more since I can't get
anymore output than what's dumped to the log.  Any ideas?  I've attached
winsvc.py and a couple other files it is dependent on.

Thanks again.

2003/10/30 15:07 Eastern Standard Time [-] Log opened.
2003/10/30 15:07 Eastern Standard Time [-]
twisted.spread.pb.PBServerFactory starting on 8787
2003/10/30 15:07 Eastern Standard Time [-] Starting factory
<twisted.spread.pb.PBServerFactory instance at 0x00DF8D28>
2003/10/30 15:08 Eastern Standard Time [-] Unexpected error in main loop.
2003/10/30 15:08 Eastern Standard Time [-] Traceback (most recent call
	  File "E:\Python22\Lib\site-packages\win32\lib\win32serviceutil.py", line 635, in SvcRun
	  File "E:\ccase\python\winsvc.py", line 24, in SvcDoRun
	  File "E:\Python22\Lib\site-packages\twisted\internet\default.py", line 122, in run
	--- <exception caught here> ---
	  File "E:\Python22\Lib\site-packages\twisted\internet\default.py", line 133, in mainLoop
	  File "E:\Python22\Lib\site-packages\twisted\internet\win32eventreactor.py", line 180, in doWaitForMultipleEvents
	    fd, action = events[handles[val - WAIT_OBJECT_0]]
	exceptions.KeyError: <PyHANDLE:224>

----- Original message -----
From: "Itamar Shtull-Trauring" <itamar at itamarst.org>
To: twisted-python at twistedmatrix.com
Date: Wed, 22 Oct 2003 14:50:10 -0400
Subject: Re: [Twisted-Python] twistd and win32 service

On Wed, 22 Oct 2003 12:24:42 -0600
"Justin Johnson" <justinjohnson at fastmail.fm> wrote:

> The general consensus on the mailing list archives seems to be that
> I'd need to setup my code to not require twistd but just run
> standalone. 

Uh. *Maybe* that's the consensus, but it's not actually correct. You can
have a tap or tac run as a NT service.

Lets say you have  a script "server.py" that is runnable with "twistd
-y", you can do (and similar code will work for TAPs):

import sys, os
import win32serviceutil, win32service

class MyService(win32serviceutil.ServiceFramework):
    """NT Service."""
    _svc_name_ = "MyService"
    _svc_display_name_ = "MyService server"

    def SvcDoRun(self):
        import server
        f = open(os.path.join(server.rootPath, "cyberhigh.log"), 'a')
        from twisted.python.log import startLogging
        from twisted.application.app import startApplication
        from twisted.internet import reactor        
        startApplication(server.application, 0)
    def SvcStop(self):
        from twisted.internet import reactor

if __name__ == '__main__':

Itamar Shtull-Trauring    http://itamarst.org/
Available for Python & Twisted consulting

Twisted-Python mailing list
Twisted-Python at twistedmatrix.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: winsvc.py
Type: application/unknown
Size: 991 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20031031/80f81b48/attachment.bin 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: tap.py
Type: application/unknown
Size: 3240 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20031031/80f81b48/attachment-0001.bin 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: server.py
Type: application/unknown
Size: 351 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20031031/80f81b48/attachment-0002.bin 

More information about the Twisted-Python mailing list