ruby on rails - Easiest way to limit thread count in a loop? -
heroku apparently limits number of threads , issues threaderror (can't create thread (11))
errors when reach limit.
what's easiest way limit number of threads defined limit, say, n
? assume loop looks this:
beers.each |abeer| threads << thread.new(abeer) { |beer| drink(beer) } end threads.each { |athread| athread.join }
you can use gem ruby-thread, it's thread pool. install gem install thread
.
require 'thread/pool' n = 4 pool = thread.pool(n) # count of threads beers.each |abeer| pool.process { drink(abeer) } end pool.shutdown
Comments
Post a Comment