Search code examples
ruby-on-railsrubymultithreadingjoinpool

How do I implmeent "join" in my Rails thread pool?


I'm using Rails 5.0 with Ruby 2.4. I have this gem

gem 'concurrent-ruby'

I want to load up a thread pool with work, but then I don't want execution to continue unti everything in the thradpool has finished executing. SO I tried this ...

  pool = Concurrent::FixedThreadPool.new(@concurrent_threads) 
  links.each do |link|
    pool.post do
      ... do work ...
    end 
  end
  pool.shutdown 

But this doesn't seem to be working. That is, there is code executed after the "pool.shutdown" line and I was hoping that would work somewhat like a ".join," that is, holding execution until everything had completed. How do I do that?


Solution

  • From the docs, after calling pool.shutdown, you can call pool.wait_for_termination to block until all threads have completed.