Skip to content

Instantly share code, notes, and snippets.

@deepak
Created January 14, 2011 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deepak/779503 to your computer and use it in GitHub Desktop.
Save deepak/779503 to your computer and use it in GitHub Desktop.
# https://github.com/rubygems/rubygems-mirror/blob/master/lib/rubygems/mirror/pool.rb
# also check
require 'thread'
class MirrorPool
def initialize(size)
@size = size
@queue = Queue.new
end
def job(&blk)
@queue << blk
end
def run_til_done
threads = Array.new(@size) do
Thread.new {
while true
@queue.pop.call
puts "done"
end
}
end
puts "queue_empty: #{@queue.empty?} num_waiting: #{@queue.num_waiting} size: #{@size}"
# what does num_waiting denote. test-case where it grows.
until @queue.empty? && @queue.num_waiting == @size
threads.each { |t| t.join(0.1) }
end
puts "queue_empty: #{@queue.empty?} num_waiting: #{@queue.num_waiting} size: #{@size}"
threads.each { |t| t.kill }
end
end
queue_size = 5
pool = MirrorPool.new(queue_size)
(1..queue_size).each do |x|
pool.job { puts x }
end
pool.run_til_done
q = Queue.new
q << Proc.new { puts 1 }
q.instance_eval { puts @que.inspect } #nil - WTF why does it not print the varible. it is initialized to []
q.instance_eval { puts @waiting.inspect } #nil - WTF why does it not print the varible. it is initialized to []
class Foo
def initialize
@foo = []
end
end
Foo.new.instance_eval { puts @foo.inspect } #[]
__END__
1
done
2
done
3
done
4
done
5
done
queue_empty: true num_waiting: 5 size: 5
queue_empty: true num_waiting: 5 size: 5
nil
nil
[]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment