2015年1月7日水曜日

【Rails】Parallelで並列処理すると"DEPRECATION WARNING: Database connections will not be closed automatically, please close your database connection at the end of the thread by calling `close` on your connection. For example: ActiveRecord::Base.connection.close" と警告がでる

 ループ処理の高速化のためParallelでマルチスレッドにすると、処理後に、「DEPRECATION WARNING: Database connections will not be closed automatically, please close your database connection at the end of the thread by calling `close` on your connection. For example: ActiveRecord::Base.connection.close」という警告がでる。
コネクションを自分で閉じろとな、ふむふむ。言わんとしていることは分かるので、警告に従いclose処理をいれる。


Parallel.map_with_index( array, :in_threads=> 4 ) do | data, idx |
begin
# やりたい処理
rescue => ex
Rails.logger.error "エラーです。"
ensure
ActiveRecord::Base.connection.close
end
end

しかし警告は消えない。ホワイ? 結局解決方法はみつからず。しかし何回動かしてもプログラムの動作には
問題ないし、接続先のDBで接続すうを見てみると。

プログラム実行時


mysql> show status like 'Threads_connected';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Threads_connected | 24 |
+-------------------+-------+
1 row in set (0.01 sec)

プログラム終了後


mysql> show status like 'Threads_connected';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Threads_connected | 14 |
+-------------------+-------+

プログラム終了後に接続数はちゃんと減っているので大丈夫なんではないだろうか。

0 件のコメント:

コメントを投稿