I'm doing the following in Ruby Eventmachine to give me a status output every 5 seconds:
def status_output
puts Time.now.to_s
EM::Timer.new(5) { status_output }
end
EM::run do
status_output
end
It's working fine, but when I'm adding "work" to eventmachine, there will be no status output any longer:
EM::run do
status_output
10_000.times do
EM::defer(proc {
# ... processing data...
})
end
end
Is this an indicator that the data processing is blocking the main thread? I'm using Ruby MRI 1.8.7 (I know, I know), so I guess a block would affect all threads (so I guess this could explain that?)
By the way, I have checked the data processing code, but there is nothing obvious that could be blocking. What are common (not-so-obvious-)things that might block?
EDIT:
I'm adding a better example as pointed out by mudasobwa: (but I'm still not sure if this is isomorph to what I'm actually running)
require "rubygems"
require "eventmachine"
def status_output
puts Time.now.to_s
EM::Timer.new(1) { status_output }
end
def process_data
500_000.times do
rand(24 ** 24).to_s(36)
end
puts "#{Time.now.to_s}: finished!"
end
EM::run do
status_output
1000.times {
EM::defer(proc {
process_data
})
}
end
# Output:
# Wed Sep 04 18:08:58 +0400 2013
# Wed Sep 04 18:09:03 +0400 2013 # Gap here
# Wed Sep 04 18:09:04 +0400 2013
# Wed Sep 04 18:09:05 +0400 2013
# Wed Sep 04 18:09:06 +0400 2013