Created
May 16, 2014 08:42
-
-
Save amcvega/93dbd8f7b31d02679c95 to your computer and use it in GitHub Desktop.
hiredis with thread pool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
require 'hiredis' | |
require 'thread' | |
class Pool | |
def initialize(size) | |
@size = size | |
@jobs = Queue.new | |
@pool = Array.new(@size) do |i| | |
Thread.new do | |
Thread.current[:id] = i | |
catch(:exit) do | |
loop do | |
job, args = @jobs.pop | |
job.call(*args) | |
end | |
end | |
end | |
end | |
end | |
def schedule(*args, &block) | |
@jobs << [block, args] | |
end | |
def shutdown | |
@size.times do | |
schedule { throw :exit } | |
end | |
@pool.map(&:join) | |
end | |
end | |
conn = Hiredis::Connection.new | |
conn.connect("127.0.0.1", 6379) | |
pool = Pool.new(2) | |
100.times do | |
pool.schedule do | |
conn.write ["GET", "fooz"] | |
print conn.read | |
print "\n." | |
end | |
end | |
pool.shutdown |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment