Created
February 8, 2017 22:26
-
-
Save shamil614/7078915084ac331ad5331ced9de6b55a to your computer and use it in GitHub Desktop.
thread queue example
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
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