错误1053:“服务未及时响应”

时间:2019-04-23 11:38:14

标签: python python-3.x

我正在尝试使Windows服务在python中。 我有三个源文件:add_to_startup.py(将我的服务添加到Windows启动),run_as_a_service.py(将我的程序作为服务运行)和test.py(我的程序)。

首先,我使用pyinstaller创建.exe:

pyinstaller -F --onefile --hidden-import=win32timezone run_as_a_service.py test.py add_to_startup.py

当我尝试启动服务时发生错误:

Error 1053: The service did not respond in a timely fashion.

如果我使用test import和test.service()函数删除字符串,则服务有效。

run_as_service.py:

import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import sys
import time
import test

class AppServerSvc (win32serviceutil.ServiceFramework):
    _svc_name_ = "test"
    _svc_display_name_ = "Test"
    _svc_description_ = "test test"

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

    def SvcStop(self):
        self.isAlive = False
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        self.isAlive = True
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                                servicemanager.PYS_SERVICE_STARTED,
                                (self._svc_name_,''))
        self.main()

    def main(self):
        while self.isAlive:
            test.service()
            time.sleep(5)


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

test.py:

import add_to_startup

def service():
    print("Hello world")

0 个答案:

没有答案