This function takes an array of objects and executes a method in each object in parallel in multiple threads
Emad Elsaid · @emad-elsaid · about 3 years
This function takes an array of objects and executes a method in each object in parallel in multiple threads
def thread_pool(objects:, method:, pool_size:)queue = Queue.newobjects.each { |obj| queue << obj }(0...pool_size).map doThread.new dowhile object = queue.pop(true)beginputs "Executing: #{object}#.#{method}"object.send(method)rescue StandardError => eputs "Error processing #{object} #{e}"endendrescue ThreadErrortrueendend.map(&:join)end