0

我必须将我的 python 脚本作为 win32 服务运行,如下所示:

服务.py

import win32service
import win32serviceutil
import win32api
import win32con
import win32event
import win32evtlogutil
import os

class aservice(win32serviceutil.ServiceFramework):
   
   _svc_name_ = "aservice"
   _svc_display_name_ = "a service - it does nothing"
   _svc_description_ = "Tests Python service framework by receiving and echoing messages over a named pipe"
         
   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)
           win32event.SetEvent(self.hWaitStop)                    
         
   def SvcDoRun(self):
      import servicemanager      
      servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,servicemanager.PYS_SERVICE_STARTED,(self._svc_name_, '')) 
      
      self.timeout = 3000
      from cron.realtimemonitorschedule import startMonitor
      startMonitor()
      while 1:
         # Wait for service stop signal, if I timeout, loop again
         rc = win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
         # Check to see if self.hWaitStop happened
         if rc == win32event.WAIT_OBJECT_0:
            # Stop signal encountered
            servicemanager.LogInfoMsg("aservice - STOPPED")
            break
         else:
            servicemanager.LogInfoMsg("aservice - is alive and well")   
               
      
def ctrlHandler(ctrlType):
   return True
                  
if __name__ == '__main__':   
   win32api.SetConsoleCtrlHandler(ctrlHandler, True)   
   win32serviceutil.HandleCommandLine(aservice)

我可以成功安装服务,但是当我启动服务时,它会提示错误如下:

from cron.realtimemonitorschedule import startMonitor
ImportError: No module named cron.realtimemonitorschedule 

请注意,cron.realtimemonitorschedule 是在 realtimemonitorschedule.py 中定义的,我的问题是如何从 win32 服务调用我的脚本?我无法在上面的 aservice.py 中嵌入我的脚本

4

1 回答 1

0

我试图弄清楚,我需要让 Python 找到我的模块。通过将路径设置为 PYTHONPATH 或将整个模块复制到 PYTHON_HOME 文件夹,后者仅用于测试。

于 2012-10-19T08:32:56.600 回答