unicorn Ruby/Rack server user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: 胡明 <humings@gmail.com>
To: unicorn-public@bogomips.org
Subject: Is it possible to create a thread in Rails subscribe to Redis message channel?
Date: Tue, 24 Mar 2015 23:28:04 +0800	[thread overview]
Message-ID: <CALFpTnNDAb-T=yuAF7eizD0Yo_kDidfHoXew+EjUVu+UOwEpCQ@mail.gmail.com> (raw)

Hi there

I have one question about related to thread in Unicorn; I have asked the
quesiton on stackoverflow.com but no one answers:


I also pasted the question in this email, hope that someone could help me
with this:


I am trying to create a thread in Rails to subscribe a message channel of
Redis. Is there a way to do this? I am using unicorn.

I have tried to do this in the unicorn configuration like this:

after_fork do |server, worker|

  Thread.new do
      $redis.subscribe(:one, :two) do |on|
        on.subscribe do |channel, subscriptions|
          puts "Subscribed to ##{channel} (#{subscriptions} subscriptions)"
        on.message do |channel, message|
          puts "##{channel}: #{message}"
          $redis.unsubscribe if message == "exit"
        on.unsubscribe do |channel, subscriptions|
          puts "Unsubscribed from ##{channel} (#{subscriptions} subscriptions)"
    rescue Redis::BaseConnectionError => error
      puts "#{error}, retrying in 1s"
      sleep 1

But it will make the unicorn server unable to handle any web request. I
thought that if I am using a different thread to subscribe to Redis, it
won't block the main thread; am I missing something here?

Hu, Ming

             reply	other threads:[~2015-03-24 15:28 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-24 15:28 胡明 [this message]
2015-03-24 22:44 ` Is it possible to create a thread in Rails subscribe to Redis message channel? Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

  List information: https://yhbt.net/unicorn/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CALFpTnNDAb-T=yuAF7eizD0Yo_kDidfHoXew+EjUVu+UOwEpCQ@mail.gmail.com' \
    --to=humings@gmail.com \
    --cc=unicorn-public@bogomips.org \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox


This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).