0

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
Benedikt B
  • 733
  • 8
  • 23
  • Are you sure the code you’ve shown is isomorph to the code you’ve actually running? No mutices etc? I’ve tried your example with `proc data = sleep 100` and yielded the proper `now` printing. – Aleksei Matiushkin Sep 04 '13 at 13:23
  • Now it’s weird that there is no `*: finished!` string put out. I guess there is no way to force the thread to switch to another one (or another thread to gain access right after exactly 1s was passed) in ruby at all. Even documentation for `Thread.pass` clearly states, that _the processor_ finally decides whether to pass an execution to other thread. That means, that optimization for power computation may spit on timers and run at once between thread switching. – Aleksei Matiushkin Sep 04 '13 at 15:39

0 Answers0