我目前正在 Julia 中编写一个简单的 IRC 机器人,以更加熟悉它,并且我制作了自己的数学工具来计算用户输入。
问题在于,它可能需要花费大量时间来运行数学问题,从而由于 ping 超时而与 IRC 服务器断开连接。
如何在表达式上运行 eval() 而不出现此问题。我在想要么限制 eval() 允许使用的时间,要么限制一些多线程。
最佳答案
您可以执行以下操作(伪代码):
addprocs(1) # once on program startup to launch a dedicated computational worker
require("my_computational_funcs.jl") # load computational functions on all processes
response = RemoteRef()
@async put!(response, remotecall_fetch(2, computational_func, args...)) # Run computation on worker 2
start=time()
while !isready(response) && (time() - start) < 30.0 # timeout of 30 seconds
sleep(0.1)
end
if !isready(response)
interrupt(2) # interrupt the computation on 2
do_error_processing()
else
do_response_processing(fetch(response))
end
关于 Julia :你能为评估设定一个时间限制吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23809515/