Skip to content

[feature] finish_in_order - process finish hook in the original order #338

@shaicoleman

Description

@shaicoleman

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions