【问题标题】:Is it possible to subclass a subclass of win32serviceutil.ServiceFramework?是否可以继承 win32serviceutil.ServiceFramework 的子类?
【发布时间】:2019-01-17 22:56:58
【问题描述】:

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

代码如下:

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 是否不能准确地将子类行为传递给 EXE 上下文?

如果是这样,以下替代策略的优缺点是什么:

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

  2. 在运行真正服务的同一台机器上运行“ServiceHelper”,在 __init__ 上为每个服务运行,您将咨询此 ServiceHelper 以获取它们的共享行为(如 DoRun 和 Stop 方法,等等)

【问题讨论】:

    标签: python python-3.x windows-services pyinstaller


    【解决方案1】:

    哇!这就是问题所在:

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

    super(BaselineService, self).__init__(args)
    

    ...我直接调用了父类,它起作用了:

    PythonServiceBase.__init__(self, args)
    

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

    super().__init__(args)
    

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

    【讨论】:

    • 在 Python3 中,在你的情况下你可以这样调用 super:super().__init__(args)。我对吗 ?我认为它应该有效。
    • 啊,这也有效@DorianTurba - 谢谢。我在Python2中学过super,在python3中用的不多
    猜你喜欢
    • 2019-07-10
    • 1970-01-01
    • 2012-05-22
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    相关资源
    最近更新 更多