Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require 'benchmark'
- # global executorの並列度を高めにしてから...
- def concurrent_download(files, future_provider: Concurrent::Future)
- require 'concurrent/future'
- # 最大20ファイルずつ並列ダウンロード
- n = 20
- download_sets = files.each_slice(n).map { |fs|
- fs.map { |f| future_provider.execute { sleep(rand); puts "Downloading #{f}"; f } }
- }
- downloaded_files = future_provider.execute {
- download_sets.inject([]) { |completed, ds|
- ds.each { |d|
- completed << d.value
- }
- completed
- }
- }.value
- end
- class Task
- attr_accessor :thread, :result
- def self.execute(&block)
- task = Task.new
- t = Thread.start(task, block) do |t, b|
- t.result = b.call
- end
- task.thread = t
- end
- def value
- thread.join
- result
- end
- end
- Benchmark.bm do |x|
- x.report do
- files = 1.upto(30).map { |num| "file#{num}" }
- r = concurrent_download(files, future_provider: Task)
- puts "Completed downloading #{files.count} files: #{r}"
- end
- end
Add Comment
Please, Sign In to add comment