【问题标题】:Error: Service instance has no attribute 'SvcDoRun'错误:服务实例没有属性“SvcDoRun”
【发布时间】:2014-03-11 18:18:19
【问题描述】:

在 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() AttributeError: Myservice instance has no attribute 'SvcDoRun'.

代码 sn-p 为:

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)

【问题讨论】:

    标签: python windows winapi service pywin32


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-28
      • 2016-06-29
      • 2012-10-07
      • 2013-02-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多