错误:服务实例没有属性' SvcDoRun'

时间:2014-03-11 18:18:20

标签: python windows winapi service pywin32

在Windows 7 64位计算机上使用python 2.7。我已经按照链接http://essiene.blogspot.in/2005/04/python-windows-services.html但在事件查看器Windows日志中看到时出现错误 回溯(最近一次调用最后一次):文件" C:\ Python27 \ lib \ site-packages \ win32 \ lib \ win32serviceutil.py",第835行,在SvcRun self.SvcDoRun()中属性错误: Myservice实例没有属性' SvcDoRun'

代码段如下:

import win32service
import win32serviceutil
import win32api
import win32con
class Myservice(win32serviceutil.ServiceFramework):
    _svc_name_ = "Myservice"
    _svc_display_name_ = "Myservice"

def __init__(self,args):
    win32serviceutil.ServiceFramework.__init__(self,args)
    self.isAlive = True

def SvcDoRun(self):
    while self.isAlive:
        if len(List)!=0:
            for i in range(0,len(List)):
                t = ThreadClass(NameList[i],name)
                t.start()

def SvcStop(self):
    import servicemanager

    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
    self.isAlive = False

def ctrlHandler(ctrlType):
    return True

if __name__ == '__main__':
    win32api.SetConsoleCtrlHandler(ctrlHandler, True)
    win32serviceutil.HandleCommandLine(Myservice)

1 个答案:

答案 0 :(得分:1)

我偶然发现了同样的问题。您需要缩进函数定义,否则它们不属于MyService类。因此,您的代码必须如下所示:

import win32service
import win32serviceutil
import win32api
import win32con
class Myservice(win32serviceutil.ServiceFramework):
    _svc_name_ = "Myservice"
    _svc_display_name_ = "Myservice"

    def __init__(self,args):
        win32serviceutil.ServiceFramework.__init__(self,args)
        self.isAlive = True

    def SvcDoRun(self):
        while self.isAlive:
            if len(List)!=0:
                for i in range(0,len(List)):
                    t = ThreadClass(NameList[i],name)
                    t.start()

    def SvcStop(self):
        import servicemanager

        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        self.isAlive = False

    def ctrlHandler(ctrlType):
        return True

if __name__ == '__main__':
    win32api.SetConsoleCtrlHandler(ctrlHandler, True)
    win32serviceutil.HandleCommandLine(Myservice)