简体   繁体   English

Python可执行文件作为Windows服务

[英]Python executable as Windows Service

I know there are similar topics to this on StackOverflow, but none of them have the same issue as me. 我知道在StackOverflow上有与此类似的主题,但是没有一个主题与我有相同的问题。 Most of the questions are asking how to start a service from Python. 大多数问题都在询问如何从Python启动服务。 I have a .bat file that is creating a service, and using a PythonFile.exe that I created using py2exe. 我有一个.bat文件,它正在创建服务,并使用我使用py2exe创建的PythonFile.exe。 I am getting the error "Error starting service. The service did not respond to the start or control request in a timely fashion". 我收到错误消息“错误启动服务。该服务未及时响应启动或控制请求”。 The service does not run, but I see the executable in the ProcessManager processes. 该服务无法运行,但是我在ProcessManager进程中看到可执行文件。

Are there specific requirements for executables to be eligible as a service? 可执行文件是否有资格获得服务的特定要求? My executable is just a TCP server that Sleeps (using a mutex) until the mutex is unlocked. 我的可执行文件只是一个TCP服务器,它会休眠(使用互斥锁)直到互斥锁解锁。

My .bat file... 我的.bat文件...

net stop "FabulousAndOutrageousOinkers"
%SYSTEMROOT%\system32\sc.exe delete "FabulousAndOutrageousOinkers"
%SYSTEMROOT%\system32\sc.exe create "FabulousAndOutrageousOinkers" binPath= "%CD%\FabulousAndOutrageousOinkers.exe" start= auto
net start "FabulousAndOutrageousOinkers"

I ended up finding an answer to my question. 我最终找到了我问题的答案。 There are in fact requirements to be a service. 实际上,要求提供服务。 Most scripts or programs that become services have a wrapper layer above the code to manage handling these requirements. 大多数成为服务的脚本或程序在代码上方都有一个包装层,用于管理对这些要求的处理。 This wrapper ends up calling the developer's code, and signaling the windows service with different types of statuses. 该包装器最终调用开发人员的代码,并以不同类型的状态向Windows服务发出信号。 Start, Stop, etc... 开始,停止等...

import win32service  
import win32serviceutil  
import win32event  

class Service(win32serviceutil.ServiceFramework):  
    # you can NET START/STOP the service by the following name  
    _svc_name_ = "FabulousAndOutrageousOinkers"  
    # this text shows up as the service name in the Service  
    # Control Manager (SCM)  
    _svc_display_name_ = "Fabulous And Outrageous Oinkers"  
    # this text shows up as the description in the SCM  
    _svc_description_ = "Truly truly outrageous"  

    def __init__(self, args):  
        win32serviceutil.ServiceFramework.__init__(self,args)  
        # create an event to listen for stop requests on  
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)  

    # core logic of the service     
    def SvcDoRun(self):  
        import servicemanager
        self.ReportServiceStatus(win32service.SERVICE_START_PENDING)  

        self.start() 
        rc = None  

        # if the stop event hasn't been fired keep looping  
        while rc != win32event.WAIT_OBJECT_0:  
            # block for 5 seconds and listen for a stop event  
            rc = win32event.WaitForSingleObject(self.hWaitStop, 5000)

        self.stop()

    # called when we're being shut down      
    def SvcStop(self):  
        # tell the SCM we're shutting down  
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)  
        # fire the stop event  
        win32event.SetEvent(self.hWaitStop)

    def start(self):
       try:
          file_path = "FabulousAndOutrageousOinkers.exe"
          execfile(file_path)             #Execute the script
       except:
          pass

    def stop(self):
      pass

if __name__ == '__main__':  
    win32serviceutil.HandleCommandLine(Service)

I found this template from http://www.chrisumbel.com/article/windows_services_in_python . 我从http://www.chrisumbel.com/article/windows_services_in_python找到了这个模板。 This code still has some issues because I get the error "Error starting service: The service did not respond to the start or control request in a timely fashion", but it still answers my question. 这段代码仍然存在一些问题,因为我收到错误消息“启动服务错误:该服务未及时响应启动或控制请求”,但它仍回答了我的问题。 There are in fact requirements for an executable to become a Windows Service. 实际上,将可执行文件变成Windows服务是有要求的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM