根据时间从块中断

chi*_*ief 2 ruby

我想跑一个街区60秒.到目前为止,我所提出的并没有按照需要突破障碍.

@start_time = Time.now
stream_some_data do |x| 
  # .. do something ...
  break if Time.now == @start_time + 60
end
Run Code Online (Sandbox Code Playgroud)

d11*_*wtq 8

Ruby的stdlib已经有了一个Timeout模块:

begin
  require "timeout"
  Timeout::timeout(60) do
    # all of the things...
  end
rescue Timeout::Error => e
end
Run Code Online (Sandbox Code Playgroud)