gpt4 book ai didi

julia - Julia 中的命令超时

转载 作者:行者123 更新时间:2023-12-05 00:23:27 31 4
gpt4 key购买 nike

我有一个 Julia 脚本,它反复调用 C++ 程序来执行优化。 C++ 程序写入一个文本文件,然后我让 Julia 读取结果并决定下一步做什么。问题是偶尔(可能是 1000 多次)C++ 程序卡住(优化可能会卡住),并且我的整个脚本无限期挂起,使得脚本很难通过所有必要的程序调用。有没有办法可以添加超时,以便如果程序在 10 分钟内未完成,我可以使用新的猜测值重新启动?

简化示例:

for k = 1:10
run(`program inputs`)
end

期望:
max_runtime = 10*60 # 10 minutes
for k = 1:10
run(`program inputs`,max_runtime)
end

选择:
max_runtime = 10*60 # 10 minutes
for k = 1:10
deadline(function,max_runtime)
end

最佳答案

怎么样:

max_runtime = 10*60 # 10 minutes
for k = 1:10
proc = spawn(`program inputs`)
timedwait(() -> process_exited(proc), max_runtime)
if process_running(proc)
kill(proc)
end
end

关于julia - Julia 中的命令超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28140293/

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