Created
January 14, 2011 11:42
-
-
Save deepak/779503 to your computer and use it in GitHub Desktop.
trying to understand https://github.com/rubygems/rubygems-mirror/blob/master/lib/rubygems/mirror/pool.rb
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
# 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