Heiko Heiko - 2 months ago 27
Ruby Question

Publish to redis is blocking my process

this is my ruby code:

require 'redis'
redis = Redis.new

Thread.new do
redis.subscribe('foo_bar') do |on|
on.message do |key, id|
puts id
end
end
end

redis.publish('foo_bar', 1337)


Can anyone tell me, why the "publish" instruction in last line is blocking my process?

Thanks

Answer

It appears that redis.subscribe blocks redis access in the entire process. So when you try to publish it locks.

Solution: trying using polling instead of subscribe

Solution 2: use several REDIS connections, i.e. create REDIS connection pool