【发布时间】:2013-05-18 01:15:12
【问题描述】:
我有一个 Rails (web) 应用程序,我还需要添加一个 (redis) pub/sub 订阅者。
下面是我需要启动的 PubsubSubscriber 类,然后应用程序启动。
redis 连接是在 resque.rb 初始化文件中创建的。我在连接后尝试了 PubsubSubscriber.new,但是当我尝试启动 rails 服务器时,它挂在:
=> Booting Thin
=> Rails 3.2.13 application starting in development on http://0.0.0.0:5000
=> Call with -d to detach
=> Ctrl-C to shutdown server
与服务器启动成功时相反:
=> Booting Thin
=> Rails 3.2.13 application starting in development on http://0.0.0.0:5000
=> Call with -d to detach
=> Ctrl-C to shutdown server
>> Thin web server (v1.5.1 codename Straight Razor)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:5000, CTRL+C to stop
知道为什么当我尝试在初始化程序中实例化 PubsubSubscriber 类时服务器会挂起吗?有更好的起点吗?
# example modified from https://github.com/redis/redis-rb/blob/master/examples/pubsub.rb
class PubsubSubscriber
def initialize
$redis.psubscribe( :channel_one ) do |on|
on.psubscribe do |event, total|
end
on.pmessage do |pattern, event, message|
# message received, kick off some workers
end
on.punsubscribe do |event, total|
end
end
end
end
【问题讨论】:
-
这很正常,订阅调用会“永远”阻塞;您应该使用 eventmachine 之类的东西或在不同的进程/线程中运行它
-
我正在使用 Thin,这听起来像是句柄对我来说是 EM 的东西,所以我不需要明确的 EM.run 调用。我试过 Thread.new { PubsubSubscriber.new } 但这也是阻塞的。有什么建议吗?
-
一个对象的构造函数不是用来做阻塞事件的地方,甚至做任何其他事情,比如运行查询等等。它应该只用于初始化一个对象。如果 Ruby 对象系统正在等待构造函数完成以便正确创建对象,我不会感到惊讶。所以试着把它移到构造函数之外,放到像
start这样的单独方法中,然后像Thread.new { PubsubSubscriber.new.start }一样运行它
标签: ruby-on-rails ruby redis publish-subscribe