Use Ractor::Port to ensure continuous processing#205
Use Ractor::Port to ensure continuous processing#205Tamada-Arino wants to merge 1 commit intopubannotation:masterfrom
Conversation
726d307 to
d128279
Compare
| # - annotations are already normal, and | ||
| # - documents exist in the database | ||
| class StoreAnnotationsCollection | ||
| THREAD_POOL_SIZE = 4 |
There was a problem hiding this comment.
このファイルの変更は必要でしょうか?
Ractor::Portをつかう変更だけでは不十分かどうか知りたいです。
そのため、StoreAnnotationsCollectionに変更を入れないときに、問題が解決しているか知りたいです。
There was a problem hiding this comment.
300MBのtgzファイルを2つ同時に処理した時、途中で止まることがありました。
バグレポートでコメントいただいたコードも、スレッド数やジョブを増やすと途中で止まっていたので、極端に処理が多いと(環境も関係していると思いますが)止まってしまう可能性があると考えています。
スレッド数を指定しないときは12件まで膨らみ、そのまま処理が止まってしまったのですが、スレッド数を指定することで比較的安定して処理を終えられました。
大容量のファイルを複数処理するためには、この変更は必要かと考えています。
There was a problem hiding this comment.
途中で止まった例:
THREADS = 10
JOBS_PER_THREAD = 100
ARRAY_SIZE = 1_000_000
def ractor_job(job_count, array_size)
port = Ractor::Port.new
workers = (1..4).map do |i|
Ractor.new(port) do |job_port|
while job = Ractor.receive
result = job.map { |x| x * 2 }.sum
job_port.send result
end
end
end
jobs = Array.new(job_count) { Array.new(array_size) { rand(1000) } }
jobs.each_with_index do |job, i|
w_idx = i % 4
workers[w_idx].send(job)
end
results = []
jobs.size.times do
puts "Waiting for results..."
_ractor, result = Ractor.select(port)
puts "Received result: #{result}"
results << result
end
results
end
threads = []
THREADS.times do
threads << Thread.new do
ractor_job(JOBS_PER_THREAD, ARRAY_SIZE)
end
end
threads.each(&:join)
puts "All threads finished."There was a problem hiding this comment.
https://bugs.ruby-lang.org/issues/21398 のコメントでもらったサンプルコードのパラメータを大きくするとRuby 3.5-dev でもRactorがhangするのでしょうが?
そうであればhangsする条件をbugs.ruby で報告すると良いと思います。
今のところメンテナの人達もRuby 3.5で直ると思っていそうです。
もしそうなら報告しないと直してもらえません。
バグに気づかれないままRuby 3.5がリリースされると私たちもメンテナも嬉しくないです。
早めに怪しい挙動を共有することでRubyの発展への貢献になります。
個人の環境に依存しているかもしれません。
bugs.rubyで報告するまえに、念のため他の誰かに確認してもらうのが良いと思います。
あとRactorの呼び出しは15行目のresult = aligner.callで終わっています。
これよりあとに作成しているスレッドはRactorに影響を与えていないはずです。
もし、この変更で動作に影響があるのであれば、Ractorとは別の問題も潜んでいそうです。
There was a problem hiding this comment.
本間さんに協力いただいたのですが、コメントのコードは(時間がかかりますが)最後まで実行できました。
また、該当のコミットを削除した状態のブランチと大きいファイルの処理について比べたところ、大きく変化はありませんでした。
該当のコミットは削除しました。
7165f26 to
decc584
Compare
decc584 to
f51a1ef
Compare
概要
Ractor::Portを使用した記述に変更しました。動作確認