在Python中创建服务包装器

时间:2019-06-07 08:08:22

标签: python-3.x

我们正试图创建一个python servicewrapper来在Windows计算机中运行python代码,但是代码由于错误而失败。我们正在使用anaconda库

            import win32service
            import win32serviceutil
            import win32event
            from application_input import appmain

            class PySvc(win32serviceutil.ServiceFramework):
                # you can NET START/STOP the service by the following name
                _svc_name_ = "ApplicationPySvc"
                # this text shows up as the service name in the Service
                # Control Manager (SCM)
                _svc_display_name_ = "Application Service"
                # this text shows up as the description in the SCM
                _svc_description_ = "This service runs Application"

                def __init__(self, args):
                    win32serviceutil.ServiceFramework.__init__(self, args)
                    # create an event to listen for stop requests on
                    self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

                # core logic of the service
                def SvcDoRun(self):
                    import servicemanager
                    appmain()

                    #import logging

                    #logger = logging.getLogger(__name__)
                    #logger.setLevel(logging.INFO)

                    # create a file handler
                    #handler = logging.FileHandler('output.log')
                    #handler.setLevel(logging.INFO)

                    # create a logging format
                    #formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
                    #handler.setFormatter(formatter)

                    # add the handlers to the logger
                    #logger.addHandler(handler)

                    rc = None

                    # if the stop event hasn't been fired keep looping
                    while rc != win32event.WAIT_OBJECT_0:
                        # block for 5 seconds and listen for a stop event
                        rc = win32event.WaitForSingleObject(self.hWaitStop, 5000)

                # called when we're being shut down
                def SvcStop(self):
                    # tell the SCM we're shutting down
                    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
                    # fire the stop event
                    win32event.SetEvent(self.hWaitStop)


            if __name__ == '__main__':
                win32serviceutil.HandleCommandLine(PySvc)

每当我们尝试运行服务包装器时,都会出现以下错误

  

D:code> python appwinservicewrapper.py启动Traceback(最新   最后调用):文件“ dbmasterwinservicewrapper.py”,第56行   win32serviceutil.HandleCommandLine(PySvc)文件   “ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ win32 \ lib \ win32serviceutil.py”,   HandleCommandLine中的第544行       serviceClassString = GetServiceClassString(cls)文件“ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ win32 \ lib \ win32serviceutil.py”,   GetServiceClassString中的第485行       fname = os.path.join(path,win32api.FindFiles(fname)[0] [8])IndexError:列表索引超出范围

0 个答案:

没有答案