Python implements a method for creating a new process in a Windows service

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.