Skip to content

Instantly share code, notes, and snippets.

@mumoshu
Created March 15, 2018 08:25
Show Gist options
  • Save mumoshu/2f84a5607c6707bc9ab8abea5f746fdc to your computer and use it in GitHub Desktop.
Save mumoshu/2f84a5607c6707bc9ab8abea5f746fdc to your computer and use it in GitHub Desktop.
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment