Skip to content

Instantly share code, notes, and snippets.

@pjb3
Created November 3, 2012 18:10
Show Gist options
  • Save pjb3/4008160 to your computer and use it in GitHub Desktop.
Save pjb3/4008160 to your computer and use it in GitHub Desktop.
I'm wondering why Celluloid/Sidekiq is more efficient than just doing this, having one Thread per worker
require 'json'
require 'redis'
redis = Redis.new
60.times do
redis.rpush "queue", ["Kernel", "sleep", 1].to_json
end
require 'json'
require 'redis'
ARGV[0].to_i.times.map do |n|
Thread.new do
redis = Redis.new
while payload = redis.blpop("queue")
queue, job = payload
class_name, message, *args = JSON.parse(job)
object = Object.const_get(class_name)
puts "Thread #{n}: #{object}.#{message}(#{args.map(&:inspect).join(', ')})"
object.send(message, *args)
end
end
end.each(&:join)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment