The example in this article describes how Python implements a new process in Windows services. Share to everyone for your reference. The implementation method is as follows:
Software to install: Python and Pywin32, I'm python-2.6.amd64, pywin32-217.win-amd64-py2.6.
File name: dma_ws.py
#!pythonimport win32serviceutil Import win32service import win32eventimport os from subprocess import Popen, Pipeimport JS Onimport Signalrun_proc = Noneclass dma_ws (win32serviceutil. Serviceframework): _svc_name_ = "Dma_ws" _svc_display_name_ = "Dma_ws" 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): f = file (' c:/dxmonitorsystem/dma.conf ') host = Json.load (f) f.close () dx SRV = Os.path.join (host[' App_path '), ' dxhttpserver.py ') Run_proc = Popen ([host[' IronPython '], dxsrv], Stdin=pipe, St Dout=pipe, Stderr=pipe, Shell=false, cwd=host[' App_path ') #这里新建进程, note that the CWD parameter is essential and if the absolute path #res, err = Run_proc.comm Unicate () #这个函数内的上面部分都是逻辑处理的部分, can be customized according to their own needs, but the following line of code any service needs win32event. WaitForSingleObject (Self.hwaitstop, win32event. INFINITE) RUN_PROC.KIll () # is used to close the child process created by the service #os. Kill (Run_proc.pid, signal. SIGTERM) If __name__== ' __main__ ': Win32serviceutil. Handlecommandline (DMA_WS)
How to use:
Create service: Python dma_ws.py Install
Start service: Python dma_ws.py start
Stop service: Python dma_ws.py stop
Hopefully this article will help you with Python programming.