gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:22:21 24 4
gpt4 key购买 nike

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

代码如下:

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 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

关于python - 是否可以继承 win32serviceutil.ServiceFramework 的子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54245485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com