我目前正在Julia中编写一个简单的IRC机器人来熟悉它,并且我在计算用户输入时做了自己的数学运算。
问题是它可能会花费数百个时间来运行数学问题,从而因ping超时而断开与IRC服务器的连接。
如何在没有此问题的情况下对表达式运行eval()。我想要么限制允许使用eval()的时间,要么限制一些多线程。
答案 0 :(得分:3)
您可以执行以下操作(伪代码):
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
答案 1 :(得分:1)
没有内置方法可以执行此操作(或任何其他类型的沙盒),但您可以通过并行工具执行此操作(请参阅docs)。粗略地说,你有类似
的东西p = addprocs(1)
ref = @spawnat p 2+2 # For example
sleep(10)
isready(ref) || interrupt(p)
fetch(r) # returns 4
这显然需要改进,但它应该给你一些东西。