Python win32 服务

发布于 2024-10-09 05:55:22 字数 1639 浏览 2 评论 0原文

我有一个最小的 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 

它工作正常,但是当我使用 py2exe 编译 service.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,
    },
 )

I have a minimal python win32 service service.py that does nothing special:

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)

When I run:

 service.py install
 service.py start 

it works fine but when I compile the service.py file with py2exe to service.exe and run the following:

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

I get this message:

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

How can I resolve this problem?

Also here the distutil code:

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,
    },
 )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

三寸金莲 2024-10-16 05:55:22

将您的: setup(console=[{"script":'Service.py'}] 替换为 setup(service=[{"script":'Service.py'}]代码>. 而不是控制台使用服务。

Replace your: setup(console=[{"script":'Service.py'}] with setup(service=[{"script":'Service.py'}]. Instead of console use service.

岁月苍老的讽刺 2024-10-16 05:55:22

尝试这个设置:

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)

try this setup:

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)
乞讨 2024-10-16 05:55:22

快速谷歌想出了这个: http://islascruz .org/html/index.php?gadget=StaticPage&action=Page&id=6

它有意大利语注释,但如果您不懂意大利语,我可以帮助您翻译一些内容。

要真正调试您的问题,我想我们需要查看您的 setup.py distutils 脚本...

A quick google came up with this: http://islascruz.org/html/index.php?gadget=StaticPage&action=Page&id=6

It has Italian comments, but I can help you translate some stuff if you don't know Italian.

To truly debug your problem, I guess we will need to see your setup.py distutils script...

笑叹一世浮沉 2024-10-16 05:55:22

您可能缺少用于查找服务所需的所有 DLL 的正确路径。通常该服务作为“本地系统”服务安装,因此您需要将 PATH 添加到系统(而不是用户)。

尝试将 c:\python27 (或 python dll 的任何路径)添加到系统路径中,重新启动计算机并检查它现在是否可以正常启动。

You probably might be missing the right PATH for finding all the DLLs required by the service. Usually the service gets installed as a 'LocalSystem' service so you need to add the PATH to the System (and not to User).

Try adding c:\python27 (or whatever the path to your python dlls is) to the SYSTEM PATH, restart the computer and check if it now starts fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文