gpt4 book ai didi

Python 避免孤儿进程

转载 作者:行者123 更新时间:2023-11-28 21:53:27 24 4
gpt4 key购买 nike

我正在使用 python 进行基准测试。这可能会花费大量时间,我想设置一个(全局)超时。我使用以下脚本(总结):

class TimeoutException(Exception):
pass
def timeout_handler(signum, frame):
raise TimeoutException()

# Halt problem after half an hour
signal.alarm(1800)
try:
while solution is None:
guess = guess()
try:
with open(solutionfname, 'wb') as solutionf:
solverprocess = subprocess.Popen(["solver", problemfname], stdout=solutionf)
solverprocess.wait()
finally:
# `solverprocess.poll() == None` instead of try didn't work either
try:
solverprocess.kill()
except:
# Solver process was already dead
pass
except TimeoutException:
pass
# Cancel alarm if it's still active
signal.alarm(0)

然而,它有时会不断产生孤儿进程,但我无法可靠地重现这种情况。有谁知道防止这种情况的正确方法是什么?

最佳答案

您只需在终止进程后等待即可。

关于Python 避免孤儿进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26314487/

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