Question

I came across a code that adds a timer with timeout 0:

EventMachine.add_timer(0) {
  ...
}

does this make sense? how this can be useful? is this any different than using next_tick?

EventMachine.next_tick {
  ...
}
Was it helpful?

Solution

Since i was curios myself i took a quick look in the Eventmachine source code:

http://eventmachine.rubyforge.org/EventMachine.html#run-class_method

where i found this inside the event loop:

...
if @next_tick_queue && !@next_tick_queue.empty?
  add_timer(0) { signal_loopbreak }
end
...

which pretty much means that when you define a next_tick internally it will use add_timer(0) {..} for it.

The only difference might be the execution order, i'm not sure which way the queued timers are executed in at this moment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top