Skip to content

Instantly share code, notes, and snippets.

@shamil614
Created February 8, 2017 22:26
Show Gist options
  • Save shamil614/7078915084ac331ad5331ced9de6b55a to your computer and use it in GitHub Desktop.
Save shamil614/7078915084ac331ad5331ced9de6b55a to your computer and use it in GitHub Desktop.
thread queue example
require "thread"
require "awesome_print"
start_time = Time.now
results = { body: [] }
queue = Queue.new
0..50.times { |x| queue.push x }
workers = (0..4).map do
Thread.new do
begin
while record = queue.pop(true)
# puts "Processing: #{record}"
results[:body] << "Record #{record}"
calc = record + 1
results[:body] << "Results #{calc}"
end
rescue ThreadError
end
end
end
workers.map(&:join)
end_time = Time.now
total_time = (end_time - start_time) / 60
puts workers
ap results[:body]
puts total_time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment