Python 如果程序在<;10秒

Python 如果程序在<;10秒,python,subprocess,Python,Subprocess,我有一个脚本,它可以安排不同程序的长时间运行,这些程序有时可以并行运行(即我可以多次调用下面的函数)。我想解雇并忘记这些孩子,因此我目前有: p = subprocess.Popen(cmd, shell=False) time.sleep(20) result = p.poll() if result is not None and result != 0: print("Child failed to start with exit code: {}".format(result))

我有一个脚本,它可以安排不同程序的长时间运行,这些程序有时可以并行运行(即我可以多次调用下面的函数)。我想解雇并忘记这些孩子,因此我目前有:

p = subprocess.Popen(cmd, shell=False)
time.sleep(20)
result = p.poll()
if result is not None and result != 0:
    print("Child failed to start with exit code: {}".format(result))
    return False
return True
这在大多数情况下都很有效,但我希望为孩子获得stdout/stderr,但前提是它无法运行10秒以上

根据我的理解,使用
Popen(…,stdout=PIPE)
意味着如果我忽略了subprocess对象,那么缓冲区将填满,子进程将挂起

我怎样才能把它修好,使它不会挂起来。是否有可能以某种方式将管道换成无管道

p = subprocess.Popen(cmd, shell=False, stdout=PIPE, stderr=PIPE)
time.sleep(20)
result = p.poll()
if result is not None and result != 0:
    print("Child failed to start with exit code: {}".format(result))
    stdout, stderr = p.communicate()
    print("STDOUT:", stdout)
    print("STDERR:", stderr)
    return False
return True

还可以安全地假设,我可以轻松地将前10秒生成的输出保存在缓冲区中。

一个简单的方法是让所有子级写入临时文件()。只需在那里而不是管道中写入,并仅在需要时读取文件(如果发生崩溃)