2

我正在使用 redis 和 ruby​​ 并尝试blpop在线程内发出 a ,以便我可以等待列表中的传入项目。

问题是块中的代码blpop似乎永远不会被调用。这是我正在运行的示例代码(ruby 1.9.3):

require 'rubygems'
require 'redis'

def start_thread
  @thread = Thread.new do
    r = Redis.new
    r.blpop("test", 0) do |key, message|
      process_message(key, message)
    end
  end
  redis = Redis.new
  redis.rpush "test", "hello world"
end

def process_message(key, message)
  @message = "#{key} was sent #{message}"
end

start_thread
@thread.join
p @message

任何帮助是极大的赞赏!

4

1 回答 1

2
require 'rubygems'
require 'redis'

def start_thread
  @thread = Thread.new do
    r = Redis.new
    key, message = r.blpop(:test, 0)
    process_message(key, message)
  end
  redis = Redis.new
  redis.rpush :test, "hello world"
end

def process_message(key, message)
  @message = "#{key} was sent #{message}"
end

start_thread
@thread.join
p @message
于 2012-09-29T08:20:35.083 回答