python控制shell执行时间,若超时则强行推出,pythonshell,def command_


def command_run(command,timeout=10):    proc = subprocess.Popen(command,bufsize=0,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)    poll_seconds = .250    deadline = time.time() + timeout    while time.time() < deadline and proc.poll() == None:        time.sleep(poll_seconds)    if proc.poll() == None:        if float(sys.version[:3]) >= 2.6:            proc.terminate()    stdout,stderr = proc.communicate()    return stdout,stderr,proc.returncode

评论关闭