我有一个烧瓶/gevent SocketIOServer,需要使其作为服务工作:
class TeleportService(win32serviceutil.ServiceFramework):
_svc_name_ = "TeleportServer"
_svc_display_name_ = "Teleport Database Backup Service"
_svc_description_ = "More info at www.elmalabarista.com/teleport"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, ''))
self.ReportServiceStatus(win32service.SERVICE_RUNNING)
runServer()
@werkzeug.serving.run_with_reloader
def runServer():
print 'Listening on %s...' % WEB_PORT
ws = SocketIOServer(('0.0.0.0', WEB_PORT),
SharedDataMiddleware(app, {}),
resource="socket.io",
policy_server=False)
gevent.spawn(runTaskManager).link_exception(lambda *args: sys.exit("important_greenlet died"))
ws.serve_forever()
但是,我不知道如何从 SvcStop 停止它,并且运行它具有奇怪的行为,即在 runserver 被杀死之后,命令行参数的服务解析发生。这意味着烧瓶服务器运行,我可以从网络浏览器访问,但服务管理器将其列为“未启动”。例如,在命令行中运行:
C:\Proyectos\TeleportServer>python service.py uninstall <--BAD PARAM, TO MAKE IT OBVIOUS
2013-02-13 16:19:30,786 - DEBUG: Connecting to localhost:9097
* Restarting with reloader
2013-02-13 16:19:32,650 - DEBUG: Connecting to localhost:9097
Listening on 5000...
Growl not available: Teleport Backup Server is started
KeyboardInterrupt <--- HERE I INTERRUPT WITH CTRL-C
Unknown command - 'uninstall'
Usage: 'service.py [options] install|update|remove|start [...]|stop|restart [...
]|debug [...]'
Options for 'install' and 'update' commands only:
--username domain\username : The Username the service is to run under
--password password : The password for the username
--startup [manual|auto|disabled] : How the service starts, default = manual
--interactive : Allow the service to interact with the desktop.
--perfmonini file: .ini file to use for registering performance monitor data
建议删除实时重新加载器,这是留下的代码。还是一样的问题
def SvcDoRun(self): servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,servicemanager.PYS_SERVICE_STARTED,(self._svc_name_, ''))
#self.timeout = 640000 #640 seconds / 10 minutes (value is in milliseconds)
self.timeout = 6000 #120 seconds / 2 minutes
# This is how long the service will wait to run / refresh itself (see script below)
notify.debug("Starting service")
ws = getServer()
while 1:
# Wait for service stop signal, if I timeout, loop again
gevent.sleep(0)
rc = win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
# Check to see if self.hWaitStop happened
if rc == win32event.WAIT_OBJECT_0:
# Stop signal encountered
notify.debug("Stopping service")
ws.kill()
servicemanager.LogInfoMsg("TeleportService - STOPPED!") #For Event Log
break
else:
notify.debug("Starting web server")
ws.serve_forever()