Python win32服务

AKM*_*AKM 3 python winapi windows-services py2exe

我有一个最小的python win32服务service.py,没有什么特别的:

import win32serviceutil
import win32service
import win32event

class SmallestPythonService(win32serviceutil.ServiceFramework):
    _svc_name_ = "SmallestPythonService"
    _svc_display_name_ = "display service"
    # _svc_description_='ddd'

    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):     
         win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)

if __name__=='__main__':
    win32serviceutil.HandleCommandLine(SmallestPythonService)
Run Code Online (Sandbox Code Playgroud)

当我跑:

 service.py install
 service.py start 
Run Code Online (Sandbox Code Playgroud)

它工作正常,但当我service.pypy2exeto 编译文件service.exe并运行以下:

service.exe install
service.exe start [or trying to restart the service  from the Services.msc]
Run Code Online (Sandbox Code Playgroud)

我收到这条消息:

Could not start the  service name service on Local Computer.
Error 1053: The service did not respond to the start or control request in a timely fashion
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

这里的distutil代码:

from distutils.core import setup
import py2exe

py2exe_options = {"includes": ['decimal'],'bundle_files': 1}

setup(console=[{"script":'Service.py'}], 
    options={"py2exe": py2exe_options}, 
    zipfile = None,
    },
 )
Run Code Online (Sandbox Code Playgroud)

And*_*ers 6

替换你:setup(console=[{"script":'Service.py'}]setup(service=[{"script":'Service.py'}].而不是控制台使用服务.