朱莉娅:您可以设置评估时间限制吗 [英] Julia: Can you set a time limit on eval

查看:86
本文介绍了朱莉娅:您可以设置评估时间限制吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在用Julia编写一个简单的IRC机器人,以使其更加熟悉,并且我制作了自己的数学工具来计算用户输入.

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.

问题在于它可能会花费大量数学时间来运行数学问题,从而由于ping超时而与IRC服务器断开连接.

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.

如何在表达式上运行eval()而不出现此问题.我在想要么限制允许使用eval()的时间,要么限制一些多线程.

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.

推荐答案

您可以执行以下操作(伪代码):

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

这篇关于朱莉娅:您可以设置评估时间限制吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆