sleep until condition is true in ruby

后端 未结 4 1659
盖世英雄少女心
盖世英雄少女心 2021-02-05 11:12

Is there any better way in Ruby to sleep until some condition is true ?

loop do 
  sleep(1)
  if ready_to_go
    break
  end
end
4条回答
  •  一向
    一向 (楼主)
    2021-02-05 11:31

    You can use the waitutil gem as described at http://rubytools.github.io/waitutil/, e.g.

    require 'waitutil'
    
    WaitUtil.wait_for_condition("my_event to happen", 
                                :timeout_sec => 30,
                                :delay_sec => 0.5) do
      check_if_my_event_happened
    end
    

提交回复
热议问题