内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

如何實現(xiàn)Python啟動、停止腳本

這篇文章給大家分享的是有關如何實現(xiàn)Python啟動、停止腳本的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

成都創(chuàng)新互聯(lián)公司是一家專注于網(wǎng)站制作、成都網(wǎng)站設計與策劃設計,金湖網(wǎng)站建設哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設10余年,網(wǎng)設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:金湖等地區(qū)。金湖做網(wǎng)站價格咨詢:13518219792

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import platform,os
import time
import subprocess
import sys
import ctypes

def get_sysinfo():
    sys = platform.system()
    return os.getpid(),sys
    
def get_path():
    p=os.path.split(os.path.realpath(__file__))  # ('D:\\workspace\\python\\src\\MySQL', 'dao.py')
    p=os.path.split(p[0])
    if not p:
        os.mkdir(p)
    return p[0]

def get_pid_path():
    return get_path() +'/tmp/yqs.pid'

def check_pid(pid = 0,osname=''):
    if pid is None or pid == 0:
        return False
    wincmd = 'tasklist /FI "PID eq %s"  /FI "IMAGENAME eq python.exe "' % str(pid)
    lincmd = 'ps ax |grep %s |grep python' % str(pid)
    cmd,size = (wincmd,150) if osname=='Windows' else (lincmd,20)
    returnstr=subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True)
    data = returnstr.stdout.read()
    return len(data) > size
    
def read_pid():
    if os.path.exists(get_pid_path()):
        try:
            with open(get_pid_path(),'r') as f:
                strpid = f.readline().strip()
                return int(strpid)
        except Exception :
            return None
    return None

def rm_pid():
    if os.path.exists(get_pid_path()):
        os.remove(get_pid_path())
        
def kill(pid):
    """kill function for Win32"""
    kernel32 = ctypes.windll.kernel32
    handle = kernel32.OpenProcess(1, 0, pid)
    return (0 != kernel32.TerminateProcess(handle, 0))
        
def check_run():
    pid,osname = get_sysinfo()
    if not os.path.exists(get_pid_path()):
        with open(get_pid_path(),'w') as f: f.write(str(pid))
        return False
    
    ''' 開始檢查 '''
    rs = check_pid(read_pid(),osname)
    if not rs : 
        with open(get_pid_path(),'w') as f: f.write(str(pid))
    return rs
        
class Control :
    def start(self):
        if check_run():
            print 'pro has run'
        else :
            print 'pro start...'
            time.sleep(1000)
    
    def stop(self):
        filePid = read_pid()
        if filePid is not None and filePid > 0:
            print 'pro has kill %s' % filePid
            kill(filePid)
            rm_pid()
        else :
            print 'Process has closed'
            
    def check(self):
        filePid = read_pid()
        if not filePid or not check_run() :
            message = "Process has closed\n"
            sys.stderr.write(message)
        else :
            message = "The process has been run, the process id:%d\n"
            sys.stderr.write(message % filePid)
                
    def helpInfo(self):
        print "usage: start|stop|check|help"
                            
if __name__ == "__main__":
    contr=Control()
    if len(sys.argv) == 2:
        param = sys.argv[1]
        if 'start' == param:
            contr.start()
        elif 'stop' == param:
            contr.stop()
        elif 'check' == param:
            contr.check()
        elif 'help' == param:
            contr.helpInfo() 
        else:
            print r"not yes cmd"
            sys.exit(2)
    else:
        print "usage: %s start|stop|check|help" % sys.argv[0]

感謝各位的閱讀!關于“如何實現(xiàn)Python啟動、停止腳本”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

分享名稱:如何實現(xiàn)Python啟動、停止腳本
轉載來源:http://www.rwnh.cn/article4/gcgeie.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設計、微信公眾號品牌網(wǎng)站建設、自適應網(wǎng)站、Google企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設
曲周县| 沽源县| 东乌| 西丰县| 天长市| 大英县| 陈巴尔虎旗| 独山县| 甘孜| 岚皋县| 鹰潭市| 彭州市| 互助| 乌拉特后旗| 阳谷县| 萨嘎县| 天全县| 芦山县| 江城| 盐亭县| 城固县| 延寿县| 虞城县| 尚志市| 突泉县| 漳州市| 阿勒泰市| 台中市| 綦江县| 南通市| 简阳市| 阳高县| 定陶县| 科技| 保山市| 永宁县| 南昌县| 丰县| 扎囊县| 增城市| 拉萨市|