首页 > 代码库 > python subprocess

python subprocess

def getResult(cmd, timeout=2):  #命令超时时间
    deadline = time.time() + timeout
    r = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    while time.time() < deadline and r.poll() is None:
        time.sleep(0.1)
    if r.poll() is None:        #检查子进程
        r.kill()
        r.wait()
        return ‘‘
    r.wait()
    r = r.stdout.read().strip()
    return r