试剂氨水的生产工艺:#102 (Support running Twisted code as NT serv...

来源:百度文库 编辑:九乡新闻网 时间:2024/04/29 16:07:31

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':

win32serviceutil.HandleCommandLine(PythonService)