1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
#!python
import win32serviceutil
import win32service
import win32event
import os
from subprocess import Popen, PIPE
import json
import signal
run_proc = None
class 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()
dxsrv = os.path.join(host['app_path'], 'DXHttpServer.py')
run_proc = Popen([host['ironpython'], dxsrv],
stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False,
cwd=host['app_path'])
#这里新建进程,注意cwd参数必不可少且要是绝对路径
#res, err = run_proc.communicate()
#这个函数内的上面部分都是逻辑处理的部分,可以根据自己的需求订制,但下面这行代码任何服务都需要
win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
run_proc.kill() # 用于关闭服务所创建的子进程
#os.kill(run_proc.pid, signal.SIGTERM)
if __name__=='__main__':
win32serviceutil.HandleCommandLine(DMA_WS)
|