是否可以在Rails创建一个线程订阅Redis消息通道?
Is it possible to create a thread in Rails subscribe to Redis message channel?
我想在Rails中创建一个线程来订阅Redis的消息通道。有没有办法做到这一点?我正在使用独角兽。
我试过在独角兽配置中这样做:
after_fork do |server, worker|
Thread.new do
begin
$redis.subscribe(:one, :two) do |on|
on.subscribe do |channel, subscriptions|
puts "Subscribed to ##{channel} (#{subscriptions} subscriptions)"
end
on.message do |channel, message|
puts "##{channel}: #{message}"
$redis.unsubscribe if message == "exit"
end
on.unsubscribe do |channel, subscriptions|
puts "Unsubscribed from ##{channel} (#{subscriptions} subscriptions)"
end
end
rescue Redis::BaseConnectionError => error
puts "#{error}, retrying in 1s"
sleep 1
retry
end
end
end
但是会使麒麟服务器无法处理任何网络请求。我想如果我使用不同的线程来订阅 Redis,它不会阻塞主线程;我在这里遗漏了什么吗?
这里的问题是ruby中的GIL; Redis 的客户端库 ruby 正在为订阅命令使用循环。
我想在Rails中创建一个线程来订阅Redis的消息通道。有没有办法做到这一点?我正在使用独角兽。
我试过在独角兽配置中这样做:
after_fork do |server, worker|
Thread.new do
begin
$redis.subscribe(:one, :two) do |on|
on.subscribe do |channel, subscriptions|
puts "Subscribed to ##{channel} (#{subscriptions} subscriptions)"
end
on.message do |channel, message|
puts "##{channel}: #{message}"
$redis.unsubscribe if message == "exit"
end
on.unsubscribe do |channel, subscriptions|
puts "Unsubscribed from ##{channel} (#{subscriptions} subscriptions)"
end
end
rescue Redis::BaseConnectionError => error
puts "#{error}, retrying in 1s"
sleep 1
retry
end
end
end
但是会使麒麟服务器无法处理任何网络请求。我想如果我使用不同的线程来订阅 Redis,它不会阻塞主线程;我在这里遗漏了什么吗?
这里的问题是ruby中的GIL; Redis 的客户端库 ruby 正在为订阅命令使用循环。