Would it be possible to add the functionality to execute things in parallel, while printing the results in the original order?
Proof of concept example:
require 'parallel'
ITEMS = ('A'..'Z').to_a
def perform_work(item)
sleep(rand) * 3
end
def print_item(item)
puts item
end
results_queue = Queue.new
# Printing thread
next_index = 0
printer_thread = Thread.new do
while next_index < ITEMS.length
index, result = results_queue.pop
# If not in order, push it back for rechecking later
next results_queue << [index, result] if index != next_index
print_item(result)
next_index += 1
end
end
Parallel.each_with_index(ITEMS, in_threads: 6) do |item, index|
perform_work(item)
results_queue << [index, item]
end
printer_thread.join