Python Win32 服务

3 投票
4 回答
7518 浏览
提问于 2025-04-16 08:50

我有一个简单的 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)

当我运行:

 service.py install
 service.py start 

它运行得很好,但当我用 py2exeservice.py 文件编译成 service.exe,然后运行以下命令:

service.exe install
service.exe start [or trying to restart the service  from the Services.msc]

我收到了这个消息:

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

我该如何解决这个问题呢?

这里还有 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,
    },
 )

4 个回答

0

我在网上快速查了一下,找到了这个链接:http://islascruz.org/html/index.php?gadget=StaticPage&action=Page&id=6

里面有意大利语的评论,不过如果你不懂意大利语,我可以帮你翻译一些内容。

要真正解决你的问题,我想我们需要看看你的 setup.py 这个脚本...

1

试试这个设置:

py2exe_options = {"includes": ['decimal'],'bundle_files': 1}
setup(
    service=[{'modules':'Service.py','cmdline_style':'pywin32','description':'your service description'}],
    options={'py2exe':py2exe_options},
    zipfile=None)
6

把你的:setup(console=[{"script":'Service.py'}] 替换成 setup(service=[{"script":'Service.py'}]。这里要用 service 代替 console。

撰写回答