Search code examples
rubyeventmachine

Is it possible to use add_periodic_timer with subsecond ticks?


Using EventMachine with Ruby, is it possible to do 0.5 seconds? Or 0.01?

The docs all say 1 second.


Solution

  • Using the following

    require 'eventmachine'
    
    Thread.new { EM::run}
    while !EM.reactor_running? ; end
    
    EM::add_periodic_timer(0.01) { puts "#{Time.now.to_f}" }
    sleep
    

    you can check the displayed numbers and verify that the difference of two consecutive ones are really close to the duration you specified to EM::add_periodic_timer. On my home pc with almost 0 load I can go down to 0.001 seconds without any problems.