如何以固定间隔调用函数?

时间:2013-09-24 13:14:25

标签: ruby timer

我想每隔五分钟调用一次特定的功能。

我在字符串变量中累积数据然后我想要一个函数来处理它。

我如何在Ruby中执行此操作?

2 个答案:

答案 0 :(得分:3)

检查rufus-scheduler

require 'rufus-scheduler'

scheduler = Rufus::Scheduler.new

scheduler.in '5m' do
  your_method(args)
end

scheduler.join
  # let the current thread join the scheduler thread

答案 1 :(得分:3)

这是一个例子,通过提供一个毒药来启用/禁用另一个线程中的函数调用:

poison = false

t = Thread.new do
  loop do
    break if poison
    your_method(args)
    sleep(5.minutes)
    redo
  end
end
# uncomment the next line if this is your main thread
#t.join