gpt4 book ai didi

python - 为什么我的 python 脚本在使用子进程时频繁终止?

转载 作者:行者123 更新时间:2023-12-01 08:13:10 24 4
gpt4 key购买 nike

我有这个代码。基本上我使用子进程在 while 循环中多次执行程序。它工作正常,但经过几次(准确地说是 5 次)后,我的 python 脚本就终止了,而且距离完成还有很长的路要走。

        while x < 50:

# ///////////I am doing things here/////////////////////

cmdline = 'gmx mdrun -ntomp 1 -v -deffnm my_sim'
args = shlex.split(cmdline)
proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = proc.communicate()[0].decode()

# ///////////I am doing things here/////////////////////
x += 1

每次我调用程序,大约需要一个小时才能完成。同时,子进程应该等待,因为根据输出,我必须执行部分代码(这就是我使用 .communicate() 的原因)。

为什么会发生这种情况?

感谢您的提前帮助!

最佳答案

子进程在后台异步运行(因为它是不同的进程),您需要使用 subprocess.wait()等待它完成。由于您有多个子流程,您可能希望等待所有子流程,如下所示:

exit_codes = [p.wait() for p in (p1, p2)]

关于python - 为什么我的 python 脚本在使用子进程时频繁终止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55110384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com