!SLIDE
EM.run do
# many things...
on_a_callback do
compute_pi_decimals(1_000_000)
end
end
!SLIDE
One thread, one process...
We are using only one core of our computer!
!SLIDE
- Split your task in many small parts and run the event loop between the parts
- Delegate the computation to another thread or process
!SLIDE
var n = 0;
var compute = function() {
var pi = compute_nth_decimal(n);
if (++n == 1000000) {
console.log(pi);
} else {
process.nextTick(compute);
}
}
!SLIDE
decimals = (1..1_000_000).to_a
EM.tick_loop do
if decimals.empty?
:stop
else
compute_nth_decimal decimals.shift
end
end
!SLIDE
operation = proc {
compute_pi_decimals(1_000_000)
}
callback = proc {|result|
puts result
}
EventMachine.defer(operation, callback)