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

时间:2019-01-17 22:56:58

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

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

代码如下:

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方法等)

1 个答案:

答案 0 :(得分:1)

Do!问题出在这里:

代替调用(仅适用于Python 2):

super(BaselineService, self).__init__(args)

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

PythonServiceBase.__init__(self, args)

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

super().__init__(args)

来源:An example