Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Queue and SizedQueue: hidden gems in the Ruby standard library

Sergio Gil
September 04, 2014

Queue and SizedQueue: hidden gems in the Ruby standard library

Slides for a lightning talk showing how to implement thread pools using this two not-very-known classes in the Ruby standard library. RUG::B (Ruby Users Group Berlin) September 2014.

Sergio Gil

September 04, 2014
Tweet

More Decks by Sergio Gil

Other Decks in Programming

Transcript

  1. Hidden gems* in the Ruby Standard Library *They're not actually

    gems, that's the joke ;) Today: Queue and SizedQueue
  2. huge_list = File.open('huge.txt') # * * This works as is

    because File (and all IO's) include Enumerable
  3. huge_list = TCPSocket.new( 'somewhere.com', 1234) # * * This works

    as is because TCPSocket (and all IO's) include Enumerable
  4. require 'thread' num_workers = 20 queue = Queue.new threads =

    num_workers.times.map do Thread.new do until (item = queue.pop) == :END do_something(item) end end end list.each { |item| queue << item } num_workers.times { queue << :END } threads.each(&:join)
  5. require 'thread' num_workers = 20 queue = SizedQueue.new(num_workers * 2)

    threads = num_workers.times.map do Thread.new do until (item = queue.pop) == :END do_something(item) end end end list.each { |item| queue << item } num_workers.times { queue << :END } threads.each(&:join)
  6. require 'thread' num_workers = 20 queue = SizedQueue.new(num_workers * 2)

    threads = num_workers.times.map do Thread.new do until (item = queue.pop) == :END do_something(item) end end end list.each { |item| queue << item } num_workers.times { queue << :END } threads.each(&:join)
  7. require 'thread' num_workers = 20 queue = SizedQueue.new(num_workers * 2)

    threads = num_workers.times.map do Thread.new do until (item = queue.pop) == :END do_something(item) end end end list.each { |item| queue << item } num_workers.times { queue << :END } threads.each(&:join) U GLY