我創建了一個Windows服務在Python(Windows XP中),在這個服務我運行一個Python服務器程序(與p = subprocess.Popen(Arg)
),當我註銷服務正在運行,但我的應用程序停止。 什麼是探針權利,其他? 有人可以幫我嗎?應用程序運行從Windows服務在python停止註銷時Windows XP
感謝
源代碼:
class PythonService(win32serviceutil.ServiceFramework):
# you can NET START/STOP the service by the following name
_svc_name_ = "PythonService"
# this text shows up as the service name in the Service
# Control Manager (SCM)
_svc_display_name_ = "server service"
# this text shows up as the description in the SCM
_svc_description_ = "This service run a server"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self,args)
# create an event to listen for stop requests on
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
servicemanager.LogInfoMsg("PythonService Service is starting")
# core logic of the service
def SvcDoRun(self):
rc = None
PathPython = "c:/python2.6/python.exe"
Script = "c:/PythonService/service.py"
sJson = "c:/PythonService/service_static.json"
Arg = [PathPython,Script,"-c",sJson]
process_arreter = 1
try :
# run a server
p = subprocess.Popen(Arg)
except OSError, why:
msgerror = "PythonService Service is not running :" + os.strerror(why.errno) + " ["+PathPython+","+Script+","+sJson+"]"
servicemanager.LogErrorMsg(msgerror)
return
# if the stop event hasn't been fired keep looping
servicemanager.LogInfoMsg("PythonService Service is running")
while rc != win32event.WAIT_OBJECT_0:
# block for 5 seconds and listen for a stop event
rc = win32event.WaitForSingleObject(self.hWaitStop, 5000)
if (p.poll()!=None):
if process_arreter:
servicemanager.LogWarningMsg("Server-Engine is stopped (failed)")
process_arreter = 0
if process_arreter:
try:
p.terminate()
servicemanager.LogInfoMsg("Server-Engine is now stopped")
except :
pass
# called when we're being shut down
def SvcStop(self):
# tell the SCM we're shutting down
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
# fire the stop event
win32event.SetEvent(self.hWaitStop)
servicemanager.LogInfoMsg("PythonService Service is stopped")
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(PythonService)`
George,我的服務以管理員權限運行,python projet安裝了管理員權限;我認爲必須陷入註銷事件,以避免服務器停止。 – louis 2012-07-23 19:56:47
我知道這將是一個奇怪的問題,但確切地說,「你如何開始和停止服務」? – 2012-07-24 06:54:27