[Twisted-Python] twisted windows service

头太晕 torrycn at gmail.com
Sat Aug 29 00:52:26 MDT 2009


On Fri, Aug 28, 2009 at 7:49 PM, 陶艺夫 <artman328 at gmail.com> wrote:

> Hi,
> I copied the code from:
> http://twistedmatrix.com/pipermail/twisted-python/2007-May/015377.html and
> it ran ok.
> And then I followed David's advice from here:
> http://twistedmatrix.com/pipermail/twisted-python/2007-May/015383.html to
> use threading.Event to control server's stop, it ran ok too. But David's
> boot_service.py just could install the service, error on start (error 1063).
> Any help?
> One more question, who can fill in David's 'py2exe' setup file more codes
> to guide me moving to next step? David, are you there? :)
>
> Thanks.
>


看你的名字,我认为我写中文你应该能看懂。
我给你段可以通用的代码吧。

首先,是一个你要编译成ntservice的模块的文件。似设abcservice.py ,内容如下

#coding=utf-8
import win32serviceutil
import os
import sys
import win32api
sys.path = sys.path + [os.path.dirname(win32api.GetModuleFileName(0))]
import server


class PowerService(win32serviceutil.ServiceFramework):
    _svc_name_ = "Your Service Name"
    _svc_display_name_ = "Your Service Display Name"
    _svc_description_ = unicode(""" 这里可以写多行的中文。
也可以写 作者:你的名字。
因为文件是utf-8的,在这里,要把中文转成gb2312,才能在windows里正常显示.
""",'utf-8').encode('gb2312')

    def SvcDoRun(self):
        server.start()

    def SvcStop(self):
        server.stop()

if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(PowerService)


用把这个程序打包成.exe以后,它就是一个可以通用的启动服务的程序了。
你的不同程序,只要在同一目录下,修改你的server.py文件,就可以了。


下面举例一个server.py

#coding=utf-8
from twisted.internet import reactor

def start():
    reactor.run(installSignalHandlers=0)

def stop():
    reactor.callFromThread(reactor.stop)


下面是py2exe打包用的setup.py文件内容

rom distutils.core import setup
import py2exe

import sys


setup(name='service name',
      version='1.0',
      author='your name',
      service=['abcservice'])

print "---Done---"


然后用python setup.py py2exe命令打包。
打包好以后,就可以直接发布了。
如果缺什么模块,直接复到到abcservice.exe文件所有目录就行。
如果你的程序修改什么,只直接修改好代码,放在目录下就行。修改程序不用重新py2exe打包。
*

*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20090829/8a3709f0/attachment.html>


More information about the Twisted-Python mailing list