Mag
Mag

Reputation: 49

Thread in Parallel gem Ruby

I am using sidekiq gem for queue. and I want to process my executing parallely inside the queue.

here is my code for queue

      def perform(disbursement_id)
         some logic...
          Parallel.each(disbursement.employee_disbursements, in_threads: 2) do |employee|
            amount = amount_format(employee.amount)
            res = unload_company_account(cmp_acc_id, amount.to_s)
            load_employee_account(employee) unless res.empty?
          end
      end

Now when I use Parallel.each() without threads it works good, but when i use Parallel.each(.., in_threads:3) it goes to busy state of queue.

Not sure why in_threads takes my queue to busy state. I am not able to resolve it.

Upvotes: 2

Views: 751

Answers (1)

Leo
Leo

Reputation: 1773

Try next to make it work

Parallel.each(disbursement.employee_disbursements, in_threads: 2) do |employee|
  ActiveRecord::Base.connection_pool.with_connection do
    amount = amount_format(employee.amount)
    res = unload_company_account(cmp_acc_id, amount.to_s)
    load_employee_account(employee) unless res.empty?
  end
end

Also, that issue go away when use map instead of each or pass attribute preserve_results as true or false. That is a bit mystery because:

def each(array, options={}, &block)
  map(array, options.merge(:preserve_results => false), &block)
end

Upvotes: 1

Related Questions