Python用 subprocess编写超时进程控制脚本

一直都寻找在Python下方便控制子进程运行时间的脚本。虽然网上有很多的好方法,但是都不能满足我的需求(也是我资质太低看别人的脚本总感觉太吃力,总有些看不明白的地方)。

下面这个脚本和网上一样利用了subprocess函数创建一个子进程控制脚本。(闲话少说,直接上菜!!!)

#!/usr/bin/python
import subprocess,time
def Test_ilo():
    ilo_ip = '10.212.236.12'
    sn_nu = 'BDSGJ11275067'
    Returncode = "over"
    cmd = 'sh ilo_test.sh %s %s' %(ilo_ip,sn_nu)
    child = subprocess.Popen(cmd,stdout = subprocess.PIPE,shell = True)
    timeout = 10
    child_pid = child.pid
    while True:
        while_begin = time.time()
        Flag = child.poll()
        print Flag
        if  Flag == 0 and timeout > 0:
            Returncode = child.stdout.read()
            print Returncode
            break

    

你可能感兴趣的:(Python)