【发布时间】:2014-12-06 12:18:12
【问题描述】:
我正在使用 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)
但是它有时会不断产生孤立进程,但我无法可靠地重新创建这种情况。有谁知道防止这种情况的正确方法是什么?
【问题讨论】:
-
第一行不应该是一个类吗?
-
@RenaeLider 是的,创建样本时出现复制错误
标签: python subprocess