Skip to content

[Rails 5] Preventing ActiveRecord "deadlock"

Masashi Yokota edited this page Aug 23, 2018 · 2 revisions

From: https://github.com/rails/rails/issues/27025

data = [1, 2, 3] 
failures = []

ActiveSupport::Dependencies.interlock.permit_concurrent_loads do # Required outside Parallel
  Parallel.map(data, in_threads: 5) do |item|
    Rails.application.executor.wrap do # Required inside Parallel
      puts "ParallelMapper instance: work_in_threads: #{item}"
      failures << CollaboratorOne.create!(item) # Requires in_threads; Similar to Queue.new
    end
  end
end

unless failures.empty?
  raise("Found the following failures: #{failures}")
end
Clone this wiki locally