I’m using celluloid’s every method to execute a block every microsecond however it seems to always call the block every second even when I specify a decimal.
interval = 1.0 / 2.0
every interval do
puts "*"*80
puts "Time: #{Time.now}"
puts "*"*80
end
I would expect this to be called every 0.5 seconds. But it is called every one second.
Any suggestions?
You can get fractional second resolution with Celluloid.
Celluloid uses the Timers gem to manage the
every, which does good floating point time math and ruby’ssleepwhich has reasonable sub-second resolution.The following code works perfectly:
And it produces the following output:
As you can see, it is not perfect, but close enough for most purposes.
If you are seeing different results, it is likely because of how long your code takes in the block you have given to
everyor other timers running and starving that particular one. I would approach it by simplifying the situation as much as possible and slowly adding parts back in to determine where the slowdown is occurring.As for microsecond resolution, I don’t think you can hope to get that far down reliably with any non-trivial code.
The trivial example:
Produces:
So as you can see, even just a base ruby version on my machine running nothing but a simple
IOline doesn’t reliably give me microsecond speeds.