PYWIN32服务中的多处理
当.EXE作为服务运行时,我的过程似乎并未创建。 该服务是用Pywin32创建的,该过程只能记录某些东西并睡觉。当.exe正常运行时,该过程确实可以正常工作,但是当它作为服务构建和安装时,它不起作用。 .EXE与Pyinstaller一起构建。 这就是我创建服务的方式:
class TestService(win32serviceutil.ServiceFramework):
_svc_name_ = "Test_SERVICE"
_svc_display_name_ = "Test_SERVICE"
_svc_description_ = "Any Description"
def SvcStop(self):
"""
Stops the server and the service
"""
# report method also sets the status of the service
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
self.ReportServiceStatus(win32service.SERVICE_STOPPED)
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STOPPED,
(TestService._svc_name_, 'SVC Stop'))
def SvcDoRun(self):
"""
Call the main method to start the service
"""
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTING,
(TestService._svc_name_, 'SVC RUN'))
try:
self.main()
except Exception: # pylint: disable=broad-except
servicemanager.LogMsg(servicemanager.EVENTLOG_ERROR_TYPE,
servicemanager.PYS_SERVICE_STOPPED,
(TestService._svc_name_,
traceback.format_exc()))
else:
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STOPPED,
(TestService._svc_name_, ''))
def main(self):
"""
Starts the service
"""
multiprocessing.freeze_support()
execute()
if len(sys.argv) == 1:
# If there is no cmdline argument, app was started by windows service manager.
# assuming here we are running 'frozen'
if not hasattr(sys, 'frozen'):
print(
"Can't start, need a frozen setup (executable created with pyinstaller) when started without arguments!\n")
win32serviceutil.usage()
sys.exit(1)
try:
# start service according to http://stackoverflow.com/questions/25770873/python-windows-service-pyinstaller-executables-error-1053,at the bottom
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(TestService)
servicemanager.StartServiceCtrlDispatcher()
except Exception as error:
# log any exception that occurs during startup
servicemanager.LogMsg(servicemanager.EVENTLOG_ERROR_TYPE,
servicemanager.PYS_SERVICE_STOPPED,
(TestService._svc_name_, error))
raise
else:
win32serviceutil.HandleCommandLine(TestService)
这是新过程的执行方法:
def execute():
proc = Process(target=log_and_sleep)
proc.start()
proc.join()
def log_and_sleep():
servicemanager.LogInfoMsg('Test')
time.sleep(10)
新过程中的日志消息将在Windows事件日志中以服务作为服务后立即出现。
谁能如何正确创建新过程以在服务内部运行?
My process does not seem to be created when the .exe runs as a service.
The service is created with pywin32 and the process should only log something and sleep. When the .exe is run normally, the process does work but when it is built and installed as a service it does not work.
The .exe is build with pyinstaller.
This is how I create a service:
class TestService(win32serviceutil.ServiceFramework):
_svc_name_ = "Test_SERVICE"
_svc_display_name_ = "Test_SERVICE"
_svc_description_ = "Any Description"
def SvcStop(self):
"""
Stops the server and the service
"""
# report method also sets the status of the service
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
self.ReportServiceStatus(win32service.SERVICE_STOPPED)
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STOPPED,
(TestService._svc_name_, 'SVC Stop'))
def SvcDoRun(self):
"""
Call the main method to start the service
"""
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTING,
(TestService._svc_name_, 'SVC RUN'))
try:
self.main()
except Exception: # pylint: disable=broad-except
servicemanager.LogMsg(servicemanager.EVENTLOG_ERROR_TYPE,
servicemanager.PYS_SERVICE_STOPPED,
(TestService._svc_name_,
traceback.format_exc()))
else:
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STOPPED,
(TestService._svc_name_, ''))
def main(self):
"""
Starts the service
"""
multiprocessing.freeze_support()
execute()
if len(sys.argv) == 1:
# If there is no cmdline argument, app was started by windows service manager.
# assuming here we are running 'frozen'
if not hasattr(sys, 'frozen'):
print(
"Can't start, need a frozen setup (executable created with pyinstaller) when started without arguments!\n")
win32serviceutil.usage()
sys.exit(1)
try:
# start service according to http://stackoverflow.com/questions/25770873/python-windows-service-pyinstaller-executables-error-1053,at the bottom
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(TestService)
servicemanager.StartServiceCtrlDispatcher()
except Exception as error:
# log any exception that occurs during startup
servicemanager.LogMsg(servicemanager.EVENTLOG_ERROR_TYPE,
servicemanager.PYS_SERVICE_STOPPED,
(TestService._svc_name_, error))
raise
else:
win32serviceutil.HandleCommandLine(TestService)
And this is the execute method with the new process:
def execute():
proc = Process(target=log_and_sleep)
proc.start()
proc.join()
def log_and_sleep():
servicemanager.LogInfoMsg('Test')
time.sleep(10)
The log message from the new process will not appear in the windows event logs as soon as it runs as a service.
Does anyone no how the new process can be created correctly to run inside the service?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论