9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# 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
begin
yield item, i
rescue => e
puts "Exception in thread:"
puts e.message
puts e.backtrace if verbose
end
if verbose
mutex.synchronize do
print "completed #{completed += 1} / #{size}\r" if completed % log_each == 0
end
end
end
end
pool.shutdown
end
|