Module: Enumerable
- Defined in:
- lib/each_in_thread.rb
Instance Method Summary collapse
Instance Method Details
#each_in_thread(concurrency: 10, log_each: 1, verbose: false) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/each_in_thread.rb', line 9 def each_in_thread(concurrency: 10, log_each: 1, verbose: false) pool = Thread.pool(concurrency) mutex = Mutex.new completed = 0 each_with_index do |item, i| pool.process do yield item, i if verbose mutex.synchronize do print "completed #{completed += 1} / #{size}\r" if completed % log_each == 0 end end end end pool.shutdown end |