python - 是否可以继承 win32serviceutil.ServiceFramework 的子类?
问题描述
我有一些服务,我想为它们创建一个共享的基类......但是当我安装一个测试子类服务时,它会失败并出现一个无用的错误。
这是代码:
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 上下文?
如果是这样,以下替代策略的优缺点是什么:
从“ServiceUtil”模块导入所有共享方法以用于我的各种服务
在运行真正服务的同一台机器上运行“ServiceHelper”,并且
__init__
对于每个服务,您将咨询此 ServiceHelper 以获取它们的共享行为(如 DoRun 和 Stop 方法等)
解决方案
嗬!这是问题所在:
而不是调用(这只适用于 Python 2):
super(BaselineService, self).__init__(args)
...我直接调用了父类并且它起作用了:
PythonServiceBase.__init__(self, args)
这也有效(我没有super
正确调用。仅限 Python 3):
super().__init__(args)
推荐阅读
- android - onCharacteristicChanged() 被多次调用
- unit-testing - 测试用例失败:参数不同!通缉:
- javascript - CKEditor 复制/粘贴图像上传但未在编辑器中显示
- codeigniter - 如何从 Codeigniter 中的 mysql 查询中仅提取 url?
- postgresql - postgres - 按单词的一部分进行搜索(节点 js)
- c# - 已排序的 MVC 实体集合
- c# - 从另一台计算机连接到本地 MySQL 路由器
- php - 在php中匹配两个数组的ID
- html - ReactJS - Window.ScrollTo() 在辅助滚动条上不起作用
- hadoop - HBase 中的部分行键扫描