Implementing windows service with Python - creating a new process in the service

From , 4 Years ago, written in Python, viewed 53 times.
URL https://pastebin.vip/view/cc638784
  1.  
  2. #!python  
  3.    
  4. import win32serviceutil  
  5. import win32service  
  6. import win32event  
  7. import os  
  8. from subprocess import Popen, PIPE  
  9. import json  
  10. import signal  
  11. run_proc = None  
  12.    
  13. class DMA_WS(win32serviceutil.ServiceFramework):  
  14.     _svc_name_ = "DMA_WS"  
  15.     _svc_display_name_ = "DMA_WS"  
  16.     def __init__(self, args):  
  17.         win32serviceutil.ServiceFramework.__init__(self, args)  
  18.         self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)  
  19.    
  20.     def SvcStop(self):          
  21.         self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)  
  22.         win32event.SetEvent(self.hWaitStop)  
  23.    
  24.     def SvcDoRun(self):  
  25.         f = file('C:/DXMonitorSystem/dma.conf')  
  26.         host = json.load(f)  
  27.         f.close()  
  28.         dxsrv = os.path.join(host['app_path'], 'DXHttpServer.py')  
  29.         run_proc = Popen([host['ironpython'], dxsrv],  
  30.                         stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False,  
  31.                         cwd=host['app_path']) #这里新建进程,注意cwd参数必不可少且要是绝对路径  
  32.         #res, err = run_proc.communicate()  
  33.         #这个函数内的上面部分都是逻辑处理的部分,可以根据自己的需求订制,但下面这行代码任何服务都需要  
  34.         win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)  
  35.         run_proc.kill() # 用于关闭服务所创建的子进程  
  36.         #os.kill(run_proc.pid, signal.SIGTERM)  
  37.    
  38. if __name__=='__main__':  
  39.     win32serviceutil.HandleCommandLine(DMA_WS)  
  40.  
  41.  
  42. #//python/5168

Reply to "Implementing windows service with Python - creating a new process in the service"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website