python 做成windows service 不完全版

前面有提到过python 在ubuntu  配置为启动服务,只需要在rc.local 中配置就好。在WINDOWS中呢,需要借助于PYWIN32扩展模块,py2exe,来完成。
程序代码 程序代码

# -*- coding:utf-8 -*-

Created on 2012-3-15
@author: Administrator
import sys
import os

packagepath = os.path.abspath('..')
if packagepath:    
    print 'can not set sys path.'
#init sys path end.

# Win32 python extensions modules
import win32serviceutil
import win32service
import win32event
import win32api
import win32process
import servicemanager

import subprocess

import thread

class iNetServerService(win32serviceutil.ServiceFramework):
    # required info
    _svc_name_ = "iNetServerServer-1.0"
    _svc_display_name_ = "iNet Server 1.0"
    # optionnal info
    _svc_description_ = "iNet Server 1.0 Service"

    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        # Create an event which we will use to wait on.
        # The "service stop" request will set this event.
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
        # a reference to the server's process
        self.terpprocess = None
        # info if the service terminates correctly or if the server crashed
        self.stopping = False

    def SvcStop(self):
        # Before we do anything, tell the SCM we are starting the stop process.
        # stop the running TERP Server: say it's a normal exit
        servicemanager.LogInfoMsg('close process pid:'+str(
        win32api.TerminateProcess(int(self.terpprocess._handle), -1)
        servicemanager.LogInfoMsg("iNet Server stopped correctly")
        # And set my event.

    def StartiNet(self):
        # The server finds now its configuration automatically on Windows
        # We start the iNet Server as an independent process, but we keep its handle
        # The server's binary must be one directory above the service's binary (when py2exe'd the python libraries shouldn' mix)
        service_dir = os.path.dirname(sys.argv[0])    
        server_dir = os.path.split(service_dir)[0]    
        server_path = os.path.join(server_dir, 'inetgarment', 'start.bat')        
        self.terpprocess = subprocess.Popen([server_path], cwd=server_dir, creationflags=win32process.Create_NO_WINDOW)
        servicemanager.LogInfoMsg(server_path+' and pid is:'+str(

    def StartControl(self,ws):
        # this listens to the Service Manager's events
        win32event.WaitForSingleObject(ws, win32event.INFINITE)
        self.stopping = True

    def SvcDoRun(self):
        # Start OpenERP Server itself
        # start the loop waiting for the Service Manager's stop signal
        thread.start_new_thread(self.StartControl, (self.hWaitStop,))
        # Log a info message that the server is running
        servicemanager.LogInfoMsg("iNet Server up and running")
        # verification if the server is really running, else quit with an error
        if not self.stopping:
            sys.exit("iNet Server check: server not running, check the logfile for more info")

if __name__=='__main__':    

准备一个 start.bat文件:
程序代码 程序代码

cd \
cd C:\iNet\inetgarment
python C:\iNet\inetgarment\

然后执行 python install


问题:可以在windows服务里配置程自动启动,但停止服务,并没有停止,原因在于调用的BAT文件,停止服务只会停止cmd,不会停止python. 所以更好的办法不是调用BAT文件,而是启动EXE文件。以后有时间再测试。

文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: python
评论: 0 | 引用: 0 | 查看次数: -
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项: