首页 > 解决方案 > 控制 Julia lang 中的资源使用

问题描述

我想构建一个软件,它会每隔一段时间自动运行一次,以对我在 Julia 磁盘中的大量文件进行排序。

我试图用下面的代码来做,但是吃光了我的资源。

while true
// if it's 6 O'clock in the morning, runs function for batch processing
end

如何限制资源使用?

标签: juliadaemon

解决方案


您可以使用Timer事件而不是使用循环。您需要做的就是定义一个回调函数,该函数接受一个Timer参数并完成您想要的工作。

julia> begin
         myfun(timer) = println("Sort Files")
         t = Timer(myfun, 2, interval = 0.2) # after 2 seconds the task will run for each 0.2 seconds interval
         wait(t) # the timer events will trigger forever if you want to stop the events you should call close(t) somewhere in the future
       end

您可以根据条件使用 停止函数中的计时器close(timer),或者稍后通过调用close(t)其他有权访问的地方来停止计时器t

使用Timers,您仍然可以继续将您的 julia 实例用于其他目的。


推荐阅读