【问题标题】:How do I call a function at fixed intervals?如何以固定间隔调用函数?
【发布时间】:2013-09-24 13:14:24
【问题描述】:

我想每五分钟调用一次特定函数。

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

我如何在 Ruby 中做到这一点?

【问题讨论】:

标签: ruby timer


【解决方案1】:

查看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

【讨论】:

    【解决方案2】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-03
      • 2011-07-03
      • 2011-01-09
      • 1970-01-01
      • 1970-01-01
      • 2016-04-05
      • 1970-01-01
      相关资源
      最近更新 更多