Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View raggi_queue.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
# 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
Something went wrong with that request. Please try again.