【发布时间】:2015-07-15 23:30:48
【问题描述】:
问题
我正在尝试使用pywin32 将 Python 脚本作为 Windows 服务启动。我可以正确安装和删除该服务,但在安装时,它的状态永远不会从“停止”改变。如何修复我的代码以使服务真正运行?
代码
#!/bin/python
import winerror
import win32event
import win32service
import win32serviceutil
SVC_RUN = 1
SVC_REMOVE = 2
class TMP_Service(win32serviceutil.ServiceFramework):
_svc_name_ = "tmp_svc_name"
_svc_display_name_ = "tmp svc disp name"
_svc_reg_class = "tmp.reg_class"
_svc_description_ = "tmp description"
def __init__(self, dut_name):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
def SvcStop(self):
self.reportServiceStatus(win32service.SERVICE_STOP_PENDING)
# I will call reactor.callFromThread(reactor.stop) here to stop the
# FTP and SCP listeners
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
self.ReportServiceStatus(win32service.SERVICE_RUNNING)
# This infinite loop simulates future behavior of my service. It will
# run a Twisted reactor to handle FTP and TCP network connections.
while True:
pass
win32event.WaitforSingleObject(self.hWaitStop, win32event.INFINITE)
def install_svc():
try:
win32serviceutil.InstallService(
TMP_Service._svc_reg_class,
TMP_Service._svc_name_,
TMP_Service._svc_display_name_,
startType=win32service.SERVICE_AUTO_START)
except win32service.error as e:
if e[0] == winerror.ERROR_SERVICE_EXISTS:
pass # ignore install error if service is already installed
else:
raise
def handle_service(svc):
if svc == SVC_RUN:
try:
win32serviceutil.StartService(TMP_Service._svc_name_)
except win32service.error as e:
if ((e[0] == winerror.ERROR_SERVICE_ALREADY_RUNNING) or
(e[0] == winerror.ERROR_ALREADY_RUNNING_LKG)):
pass # ignore failed start if the service is already running
elif e[0] == winerror.ERROR_SERVICE_DOES_NOT_EXIST:
# if the service is not installed, install it and try again
install_svc()
win32serviceutil.StartService(TMP_Service._svc_name_)
else:
# reraise any other start expection
raise
status = win32serviceutil.QueryServiceStatus(TMP_Service._svc_name_)
print("Service status: {}".format(status[1]))
else:
try:
win32serviceutil.RemoveService(TMP_Service._svc_name_)
except win32service.error as e:
if e[0] == winerror.ERROR_SERVICE_DOES_NOT_EXIST:
pass # ignore failed removal if service is not installed
else:
# reraise any other remove exception
raise
if __name__ == "__main__":
handle_service(SVC_RUN)
其他详情
-
当我查看系统事件日志时,我看到了这个错误:
Python 无法导入服务的模块
ImportError:没有名为 tmp 的模块
%2: %3这些消息的时间戳与我尝试运行此脚本的时间相匹配。
我看过这个问题:Can't start Windows service written in Python (win32serviceutil)。根据那里的建议,我使我的代码与那里的最佳答案中的建议相匹配,并确保 C:\Python27 在我的系统路径中。这两种建议都没有任何作用。
打印的状态始终为“2”。根据MSDN,这是SERVICE_START_PENDING,表示服务应该正在启动。
更新详情
如果我将
_svc_reg_class_属性的值更改为"tmp2.reg_class",则Windows 事件日志中报告的缺失模块的名称将更改为“tmp2”,因此此错误与_svc_reg_class_属性。回复下面的评论:我认为我无法捕获完整的回溯,因为我的服务类已实例化并在 pywin32 库代码中使用。有问题的导入不会发生在我的代码中的任何地方,所以我没有什么可以包装在 try/except 块中。
【问题讨论】:
-
你能显示那个 ImportError 的回溯吗?您可以使用 try 将其打印到文件中,除了
import traceback; traceback.print_exc(...) -
@User 查看更新的详细信息。
-
如果您将文件命名为
tmp.py或tmp2.py,会发生什么?也许 reg 类与模块名称有关。另外,尝试将其放入站点包中。
标签: python python-2.7 windows-services pywin32