Heiko Heiko - 10 months ago 99
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

redis.publish('foo_bar', 1337)

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


Answer Source

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