我试图在Rails中使用ActiveRecord并行更新Postgres中的条目,我的代码看起来像这样。
# new_records is the result of an ActiveRecord query.
Parallel.each(new_records, in_processes: 8) do |record|
ActiveRecord::Base.connection_pool.with_connection do
ActiveRecord::Base.transaction do
edit_record(record)
record.save!
end
end
end
当我运行它时,我收到以下错误:
ActiveRecord::StatementInvalid:
PG::ConnectionBad: PQconsumeInput() server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
任何人都可以帮我解决如何正确运行ActiveRecord事务的问题吗?
答案 0 :(得分:3)
I got a similar problem when trying to do a parallel for ActiveRecord
connection pool. I solve it by changing the worker type, from processes to threads. According to the parallel
documentation, it is recomended to use threads if you are going to use explicity the ActiveRecord
connection pool.
Your code could be rewriten to:
Parallel.each(new_records, in_threads: 8) do |record|
ActiveRecord::Base.connection_pool.with_connection do
ActiveRecord::Base.transaction do
edit_record(record)
record.save!
end
end
end
I hope you find this helpful.