最简单的方法来限制循环中的线程数?

时间:2013-08-16 02:34:42

标签: ruby-on-rails ruby multithreading heroku concurrency

Heroku显然会限制线程数,并在达到限制时发出ThreadError (can't create Thread (11))错误。

将线程数限制为定义限制的最简单方法是什么,比如N?假设循环如下所示:

beers.each do |aBeer|
   threads << Thread.new(aBeer) { |beer|
      drink(beer)
   }
end
threads.each { |aThread|  aThread.join }

2 个答案:

答案 0 :(得分:4)

您可以使用gem ruby-thread,它是线程池。使用gem install thread安装。

require 'thread/pool'
N = 4
pool = Thread.pool(N) # COUNT OF YOUR THREADS 

beers.each do |aBeer|
   pool.process {
     drink(aBeer)
   }
end

pool.shutdown

答案 1 :(得分:2)

显然,Heroku限制具有固定数量的线程(256),并且还受内存限制(512或1024 MB,具体取决于您的选择)。

https://devcenter.heroku.com/articles/limits#dyno-memory


我会这样做,因为啤酒在每个线程之间公平地重新分配。

thread_limit = 10 # Here you define your thread limit
beers_per_thread = beers.count / thread_limit # Number of beers / thread 
thread_pool = nil
beers.each_with_index.map {|beer, index| 
    if index % beers_per_thread == 0
        puts "New Thread Created at beer #{index}, hips.."
        thread_pool << thread
        thread = Thread.new
    end
    # Thread stuff
}

thread_pool.each do |thread|
    # process thread
end

加上Bigxiang thread_pool,我觉得它很完美。