【问题标题】:pywin32 service start error: no module named 'tmp'pywin32 服务启动错误:没有名为“tmp”的模块
【发布时间】: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)

其他详情

  1. 当我查看系统事件日志时,我看到了这个错误:

    Python 无法导入服务的模块
    ImportError:没有名为 tmp 的模块
    %2: %3

    这些消息的时间戳与我尝试运行此脚本的时间相匹配。

  2. 我看过这个问题:Can't start Windows service written in Python (win32serviceutil)。根据那里的建议,我使我的代码与那里的最佳答案中的建议相匹配,并确保 C:\Python27 在我的系统路径中。这两种建议都没有任何作用。

  3. 打印的状态始终为“2”。根据MSDN,这是SERVICE_START_PENDING,表示服务应该正在启动。

更新详情

  1. 如果我将_svc_reg_class_ 属性的值更改为"tmp2.reg_class",则Windows 事件日志中报告的缺失模块的名称将更改为“tmp2”,因此此错误与_svc_reg_class_ 属性。

  2. 回复下面的评论:我认为我无法捕获完整的回溯,因为我的服务类已实例化并在 pywin32 库代码中使用。有问题的导入不会发生在我的代码中的任何地方,所以我没有什么可以包装在 try/except 块中。

【问题讨论】:

  • 你能显示那个 ImportError 的回溯吗?您可以使用 try 将其打印到文件中,除了 import traceback; traceback.print_exc(...)
  • @User 查看更新的详细信息。
  • 如果您将文件命名为tmp.pytmp2.py,会发生什么?也许 reg 类与模块名称有关。另外,尝试将其放入站点包中。

标签: python python-2.7 windows-services pywin32


【解决方案1】:

如下所示更改_svc_reg_class= "tmp.reg_class"。

try:
    module_path = modules[TMP_Service.__module__].__file__
except AttributeError:
    # maybe py2exe went by
    from sys import executable
    module_path = executable

module_file = splitext(abspath(module_path))[0]
TMP_Service._svc_reg_class = '%s.%s' % (module_file, TMP_Service.__name__)

以下是原始代码的完整修改版本。

#!/bin/python

import winerror
import win32event
import win32service
import win32serviceutil
from sys import modules
from os.path import splitext, abspath

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, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        self.Stop = True
        # I will call reactor.callFromThread(reactor.stop) here to stop the 
        # FTP and SCP listeners
        win32event.SetEvent(self.hWaitStop)
        self.ReportServiceStatus(win32service.SERVICE_STOPPED)

    def SvcDoRun(self):
        self.Stop = False
        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 self.Stop == False:
            pass 

        win32event.WaitforSingleObject(self.hWaitStop, win32event.INFINITE)

def install_svc():

    try:
        module_path = modules[TMP_Service.__module__].__file__
    except AttributeError:
        # maybe py2exe went by
        from sys import executable
        module_path = executable

    module_file = splitext(abspath(module_path))[0]
    TMP_Service._svc_reg_class = '%s.%s' % (module_file, TMP_Service.__name__)

    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)

参考:here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-02
    • 2019-02-23
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多