如何使用EM :: Synchrony :: Multi与redis?

时间:2012-11-29 05:16:24

标签: ruby asynchronous concurrency eventmachine em-synchrony

我试图并行执行几个redis查询,但收到错误:

/var/lib/gems/1.9.1/gems/em-synchrony-1.0.2/lib/em-synchrony.rb:53:in `resume':
dead fiber called (FiberError)

这是代码:

require 'em-synchrony'
require 'redis/connection/synchrony'
require 'redis'

redis = EM::Synchrony::ConnectionPool.new(size: 4) do
  Redis.new
end

EM.synchrony do
  multi = EM::Synchrony::Multi.new
  multi.add :a, redis.alpush('foo', 1)
  multi.add :b, redis.alpush('foo', 2)
  res = multi.perform

  p res

  EM.stop
end

请帮助

1 个答案:

答案 0 :(得分:1)

使用Eventmachine特定的em-hiredis gem而不是默认驱动程序。

gem install em-hiredis

require 'em-synchrony'
require 'em-synchrony/em-hiredis'

redis = EM::Synchrony::ConnectionPool.new(size: 4) do
          EM::Hiredis.new
        end

使用任何地方EM::Synchrony,通常建议使用em-synchrony附带的同步特定库。这就是声明em-synchrony/em-hiredis而非require em-synchrony

的原因