Julia: Can you set a time limit on eval

拟墨画扇 提交于 2020-01-24 17:35:06

问题


I am currently writing a simple IRC bot in Julia to get more familiar with it, and I made my own math thing for calculating users input.

The problem is that it can take math problems that take insane amounts of time to run, and thus disconnecting from the IRC server due to ping timeout.

How would I run eval() on a expression, without this problem. I was thinking either limiting the time eval() is allowed to use, or some multi threading.


回答1:


You could do the following (pseudo code):

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



回答2:


There's no built-in way to do this (or any other kind of sandboxing) but you may be able to do it via the parallelism tools (see the docs). Roughly speaking, you'd have something like

p = addprocs(1)
ref = @spawnat p 2+2 # For example
sleep(10)
isready(ref) || interrupt(p)
fetch(r) # returns 4

This obviously needs refinement, but it should give you something to go on.



来源:https://stackoverflow.com/questions/23809515/julia-can-you-set-a-time-limit-on-eval

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!