unicorn Ruby/Rack server user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
* Is it possible to create a thread in Rails subscribe to Redis message channel?
@ 2015-03-24 15:28 胡明
  2015-03-24 22:44 ` Eric Wong
  0 siblings, 1 reply; 2+ messages in thread
From: 胡明 @ 2015-03-24 15:28 UTC (permalink / raw)
  To: unicorn-public

Hi there

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

http://stackoverflow.com/questions/29180275/is-it-possible-to-create-a-thread-in-rails-subscribe-to-redis-message-channel

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
    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
  endend

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?

-- 
Regards,
Hu, Ming


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Is it possible to create a thread in Rails subscribe to Redis message channel?
  2015-03-24 15:28 Is it possible to create a thread in Rails subscribe to Redis message channel? 胡明
@ 2015-03-24 22:44 ` Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2015-03-24 22:44 UTC (permalink / raw)
  To: 胡明; +Cc: unicorn-public

胡明 <humings@gmail.com> wrote:
> 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.

Theoretically, yes, similar things are done with other services.

<snip>

> 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?

I'm not familiar with Redis, but I know it's a server based on stream
sockets similar to anything else HTTP/IMAP/MySQL/Postgres/memcached-based.

But based on your observation, your client library is probably blocking
the entire Ruby VM by not releasing the GVL when it the thread waits on
Redis.

Which Redis client library are you using?

If the client library is written in C, it should have rb_thread_* calls
when it needs to wait on the server for anything (e.g.
rb_thread_call_without_gvl, rb_thread_fd_select, rb_thread_fd_select,
etc...).  If you don't see those in the source code, get it fixed :)

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-03-24 22:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-24 15:28 Is it possible to create a thread in Rails subscribe to Redis message channel? 胡明
2015-03-24 22:44 ` Eric Wong

Code repositories for project(s) associated with this public inbox

	https://yhbt.net/unicorn.git

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).