【问题标题】:What does win32event.SetEvent(self.hWaitStop) do in this python windows service example?win32event.SetEvent(self.hWaitStop) 在这个 python windows 服务示例中做了什么?
【发布时间】:2019-10-09 08:29:17
【问题描述】:

我正在查看how to make a windows service 上的以下示例。

'''
SMWinservice
by Davide Mastromatteo

Base class to create winservice in Python
-----------------------------------------

Instructions:

1. Just create a new class that inherits from this base class
2. Define into the new class the variables
   _svc_name_ = "nameOfWinservice"
   _svc_display_name_ = "name of the Winservice that will be displayed in scm"
   _svc_description_ = "description of the Winservice that will be displayed in scm"
3. Override the three main methods:
    def start(self) : if you need to do something at the service initialization.
                      A good idea is to put here the inizialization of the running condition
    def stop(self)  : if you need to do something just before the service is stopped.
                      A good idea is to put here the invalidation of the running condition
    def main(self)  : your actual run loop. Just create a loop based on your running condition
4. Define the entry point of your module calling the method "parse_command_line" of the new class
5. Enjoy
'''

import socket

import win32serviceutil

import servicemanager
import win32event
import win32service


class SMWinservice(win32serviceutil.ServiceFramework):
    '''Base class to create winservice in Python'''

    _svc_name_ = 'pythonService'
    _svc_display_name_ = 'Python Service'
    _svc_description_ = 'Python Service Description'

    @classmethod
    def parse_command_line(cls):
        '''
        ClassMethod to parse the command line
        '''
        win32serviceutil.HandleCommandLine(cls)

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

    def SvcStop(self):
        '''
        Called when the service is asked to stop
        '''
        self.stop()
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        '''
        Called when the service is asked to start
        '''
        self.start()
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                              servicemanager.PYS_SERVICE_STARTED,
                              (self._svc_name_, ''))
        self.main()

    def start(self):
        '''
        Override to add logic before the start
        eg. running condition
        '''
        pass

    def stop(self):
        '''
        Override to add logic before the stop
        eg. invalidating running condition
        '''
        pass

    def main(self):
        '''
        Main class to be ovverridden to add logic
        '''
        pass

# entry point of the module: copy and paste into the new module
# ensuring you are calling the "parse_command_line" of the new created class
if __name__ == '__main__':
    SMWinservice.parse_command_line()

现在我的问题是win32event.SetEvent(self.hWaitStop) 到底做了什么?我读到了create eventset event 函数。我的猜测是程序正试图向 ServiceController 发出等待的信号?但为了什么?我觉得我在这里缺少一些上下文来完全理解 python 服务示例以及 win32event.SetEvent(self.hWaitStop) 行的确切作用。

【问题讨论】:

    标签: python windows-services


    【解决方案1】:

    我认为它没有任何作用。它位于the pywin32 service examples 中,并且已经使用了 10 年,但据我所知,图书馆内部没有使用此类事件。 win32event.SetEvent 应该得到一个线程(在win32event.CreateEvent(None,0,0,None) 中,第一个零表示the event will auto-reset 一旦线程停止等待,所以它真的是一个线程waiting for that event 继续,但除非我错过了服务如何工作的一些东西,没有任何东西等待该事件被触发,所以它不会做任何事情。

    socket.setdefaulttimeout(60) 也是如此,真的,我认为有人只是在发布到 stackoverflow 之前忘记删除它,并且每个人都在其他人没有的东西之上构建,没有人知道为什么它不再存在并且没有人曾经敢查。您可以删除整个 __init__ 函数。


    编辑:参见this question中的示例

    import win32serviceutil 
    import win32service 
    import win32event
    
    class SmallestPythonService(win32serviceutil.ServiceFramework):
        _svc_name_ = "SmallestPythonService"
        _svc_display_name_ = "display service"
    
        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)
    

    似乎这通常意味着您可以拥有一个运行直到停止的示例服务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-02
      • 1970-01-01
      • 1970-01-01
      • 2014-01-14
      • 2022-08-04
      • 2016-02-17
      • 2019-05-15
      相关资源
      最近更新 更多