0

我有使用 servicemanager 的 python 项目,我使用 pyinstaller 将其构建为 exe。一切正常,我已将其安装为窗口服务。

我设置了服务启动类型:自动(延迟启动)。

但是当我重新启动计算机时,服务没有启动,我必须去服务并手动启动它。

我检查事件查看器,它说:

The instance's SvcRun() method failed 
<Error getting traceback - traceback.print_exception() failed 
%2: %3

这是我的代码:

app = tornado.web.Application([
(r'/', WebSocketHandler),
])

class TestService(win32serviceutil.ServiceFramework):
    _svc_name_ = "SignMatchService"
    _svc_display_name_ = "SignMatchService"
    _svc_description_ = "The service match signature"

    def log(self, msg):
        import servicemanager
        servicemanager.LogInfoMsg(str(msg))

    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
        socket.setdefaulttimeout(150)

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)
        _init_asyncio_patch()
        tornado.ioloop.IOLoop.instance().stop()
        self.ReportServiceStatus(win32service.SERVICE_STOPPED)

    def SvcDoRun(self):
        global sig_matcher
        self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
        try:
            self.ReportServiceStatus(win32service.SERVICE_RUNNING)
            _init_asyncio_patch()
            asyncio.set_event_loop(asyncio.new_event_loop())
            app.listen(5555)
            tornado.ioloop.IOLoop.instance().start()
            win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
        except Exception as x:
            print('Exception : %s\n' % x)
            self.SvcStop()

if __name__ == '__main__':
    if len(sys.argv) == 1:
        servicemanager.Initialize()
        servicemanager.PrepareToHostSingle(TestService)
        servicemanager.StartServiceCtrlDispatcher()
    else:
        win32serviceutil.HandleCommandLine(TestService)
4

1 回答 1

1

抱歉信息有误。上面的错误是我测试服务时的旧错误。

错误是:

A timeout was reached (30000 milliseconds) while waiting for the TestService service to connect.

并且增加窗口服务的超时将解决问题

于 2020-05-22T08:51:39.713 回答