是否可以继承 win32serviceutil.ServiceFramework 的子类?

Is it possible to subclass a subclass of win32serviceutil.ServiceFramework?

我有一些服务,我想为它们创建一个共享基础 class...但是当我安装一个测试子 class 服务时,它失败并出现一个无用的错误。

代码如下:

import servicemanager
import win32serviceutil
import sys
import win32service
import win32event

class PythonServiceBase(win32serviceutil.ServiceFramework):
    _svc_name_ = "SmallestPythonService"
    _svc_display_name_ = "The smallest possible Python Service"
    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        # Create an event which we will use to wait on.
        # The "service stop" request will set this event.
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

    def SvcStop(self):
        # Before we do anything, tell the SCM we are starting the stop process.
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        # And set my event.
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        # We do nothing other than wait to be stopped!
        win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)


class BaselineService(PythonServiceBase):
    _svc_name_ = "SmallestPythonService2"
    _svc_display_name_ = "The smallest possible Python Service2"
    def __init__(self, args):
        super(BaselineService, self).__init__(args)


if __name__ == '__main__':
    if len(sys.argv) == 1:
        servicemanager.Initialize()
        servicemanager.PrepareToHostSingle(BaselineService)
        servicemanager.StartServiceCtrlDispatcher()
    else:
        win32serviceutil.HandleCommandLine(BaselineService)

以下是我创建和安装的方法:

pyinstaller -F --debug --hidden-import=win32timezone Baseline\baseline_service.py

dist\baseline_service.exe install && net start SmallestPythonService2

CMD 输出中没有回溯,但这显示:

The The smallest possible Python Service2 service is starting.
The The smallest possible Python Service2 service could not be started.

A service specific error occurred: 1.

More help is available by typing NET HELPMSG 3547.

在事件查看器中我看到了这个:

- <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
- <System>
  <Provider Name="Python Service" /> 
  <EventID Qualifiers="49152">10</EventID> 
  <Level>2</Level> 
  <Task>0</Task> 
  <Keywords>0x80000000000000</Keywords> 
  <TimeCreated SystemTime="2019-01-17T22:43:16.954038000Z" /> 
  <EventRecordID>76457</EventRecordID> 
  <Channel>Application</Channel> 
  <Computer>DESKTOP-IM94TC5</Computer> 
  <Security /> 
  </System>
- <EventData>
  <Data><Error getting traceback - traceback.print_exception() failed</Data> 
  </EventData>
  </Event>

难道 pyinstaller 不能准确地将 subclass 行为传递给 EXE 上下文吗?

如果是,以下替代策略的pros/cons是什么:

  1. 正在从 "ServiceUtil" 模块导入所有共享方法以用于我的各种服务

  2. 运行一个"ServiceHelper"在真正服务运行的同一台机器上,每个服务__init__,你会咨询这个 ServiceHelper 来取回他们的共享行为(如 DoRun 和 Stop 方法等)

呸!这是问题所在:

而不是调用(这仅适用于 Python 2):

super(BaselineService, self).__init__(args)

...我直接给父 class 打电话,它起作用了:

PythonServiceBase.__init__(self, args)

这也有效(我没有正确调用 super。仅 Python 3):

super().__init__(args)

来源:Python extending with - using super() Python 3 vs Python 2