Module: Enumerable

Defined in:
lib/sitemap_gen/xml_crawler.rb

Instance Method Summary collapse

Instance Method Details

#with_multithread(thread_num) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/sitemap_gen/xml_crawler.rb', line 2

def with_multithread(thread_num)
  queue = Queue.new
  threads = (1..thread_num).map do
    Thread.new do
      until queue.empty?
        begin
          yield(queue.pop)
        rescue Exception
          nil
        end
      end
    end
  end

  each { |v| queue << v }
  threads.each { |t| t.join }
end