0

示例代码:

require 'eventmachine'

t = Thread.new { EM.run }

op = proc {
  op2 = proc {
    sleep 1
    puts 'op2 finishing'
  }
  cb2 = proc {
    sleep 1
    puts 'cb2 finishing'
  }
  EM.defer(op2, cb2)

  puts 'op finishing'
}
cb = proc {
  puts 'cb finishing'
}

EM.defer(op, cb)

while (!EM.defers_finished?) do
  puts 'defers not finished, sleeping...'
  sleep 1
end

EM.stop

上述程序在大约 10% 的情况下会失败:

eventmachine-1.0.3/lib/eventmachine.rb:1039:in `signal_loopbreak': eventmachine not initialized: evma_signal_loopbreak (RuntimeError)
eventmachine-1.0.3/lib/eventmachine.rb:1039:in `block in spawn_threadpool'

即使仍有未完成的延迟,EM.defers_finished?() 似乎有时也会返回 true。我是 EventMachine 的新手,所以可能做错了,但就目前而言,EventMachine 的某些行为似乎不正确。

关于我为什么不做典型的“EM.run do”的一些背景,这是在乘客进程下运行的,因此您无法以通常的方式启动 EventMachine。请参阅http://www.railstips.org/blog/archives/2011/05/04/eventmachine-and-passenger/http://www.hiringthing.com/2011/11/04/eventmachine-with-rails。 html

4

1 回答 1

2

这里的链接,它说你需要这一行:

# Wait for the reactor to start
while not EM.reactor_running?; end

..您的错误似乎表明您缺少类似的东西:

eventmachine not initialized
于 2013-08-18T18:51:29.133 回答