我正在尝试使用赛璐珞异步处理一些 .csv 数据。我已经读过,使用期货可以让您在主线程终止之前等待一组参与者完成。我看过一些例子来证明这一点。
但是,当我在示例代码中实现它时,事实证明使用期货并不比同步处理快。谁能看到我做错了什么?
require 'smarter_csv'
require 'celluloid/current'
require 'benchmark'
class ImportActor
include Celluloid
def process_row(row)
100000.times {|n| n}
end
end
def do_all_the_things_with_futures
pool = ImportActor.pool(size: 10)
SmarterCSV.process("all_the_things.csv").map do |row|
pool.future(:process_row,row)
end.map(&:value)
end
def do_all_the_things_insync
pool = ImportActor.pool(size: 10)
SmarterCSV.process("all_the_things.csv") do |row|
pool.process_row(row)
end
end
puts Benchmark.measure { do_all_the_things_with_futures}
puts Benchmark.measure { do_all_the_things_insync }
2.100000 0.030000 2.130000 ( 2.123381)
2.060000 0.020000 2.080000 ( 2.069357)
【4.6s完成】