From: Macario Ortega Date: 2009-08-09T06:47:48+09:00 Subject: Re: Soft realtime with EventMachine and timer resolution Bill Kelly wrote: > From: "Macario Ortega" >> evma_set_timer_quantum(16/*msec*/); // roughly 60Hz > Hehe, i thought that looked familiar... it appears to be from an email > I posted on the eventmachine-talk mailing list in 2006. :) > > But yes: these days the set_timer_quantum API is available from ruby. > > EventMachine.set_timer_quantum(10); > > Note: Internally, EventMachine places limits on the min/max allowed > values for quantum: (in em.cpp) > > /* We get a timer-quantum expressed in milliseconds. > * Don't set a quantum smaller than 5 or larger than 2500. > */ > > if ((interval < 5) || (interval > 2500)) > throw std::runtime_error ("invalid timer-quantum"); > > > Hope this helps, > > Bill Thanks for your reply, it was useful! I found the cheapest is to exclusivelly use add_periodic_timer with my desired interval but it increasingly drifted. This is my timer loop: def run @next = Time.now.to_f @thread = Thread.new do EventMachine.run do @start = Time.now.to_f EM.set_quantum 5 EM.add_periodic_timer @interval / 30 do if Time.now.to_f >= @next bang @tick += 1 @next = @tick * @interval + @start end end end end self end EventMachine doesn't seem to warranty the accuracy of the periodic timers. -- Posted via http://www.ruby-forum.com/.