我期待使用多线程来加速这个过程是非常I/O绑定。我希望能够通过循环中的CSV文件中的ID来调用休息服务。我一直没能搞清楚的是如何将文件优雅分成取决于线程我想使用的量块。红宝石:一个文件,根据主题的拆分和阅读部分计数
CSV文件包含ID的一列像这样: ...
require 'benchmark'
require 'csv'
FILE_RECORD_COUNT = File.open("path-to-csv","r").readlines.size
def setup(thread_count)
threads = []
thread_count.times do
threads << Thread.new do
fetches_per_thread = FILE_RECORD_COUNT/thread_count
fetches_per_thread.times do
CSV.foreach("id_file.csv") do |id|
response = RestClient.get("https://api.examplerest/names?id={#id}",{accept: :json})
# do some quick validation...
end
end
end
end
threads.each(&:join)
end
def run_benchmark
Benchmark.bm(20) do |bm|
[1, 2, 3, 5, 6, 10, 15, 30, 100].each do |thread_count|
bm.report("with #{thread_count} threads") do
setup(thread_count)
end
end
end
end
凡我难倒,并在那里我需要一个解决方案是代码块CSV.foreach(id_file.csv") do |id|...
。我想动态分割数据并将其馈入每个线程,然后再打一个电话。我知道我可以手动分割文件,但我想避免这种情况。
我曾经尝试到从一个例子,我在网上找到的,看看那里的甜区是线程数的基准这一点。
编辑: 使用BernardK的回答,我能够运行我的代码,螺纹和将得到以下结果:
| user | system | total | real |
with 1 threads 5.125000 2.594000 7.719000 (40.416162)
with 2 threads 1.625000 2.015000 3.640000 (28.571521)
with 3 threads 1.578000 1.625000 3.203000 (17.210526)
with 4 threads 1.578000 1.235000 2.813000 ( 8.496068)
with 5 threads 1.406000 1.250000 2.656000 ( 6.779216)
with 10 threads 1.875000 1.328000 3.203000 ( 5.069487)
with 15 threads 2.016000 1.640000 3.656000 ( 4.285426)
with 30 threads 2.125000 1.625000 3.750000 ( 3.817084)
with 100 threads 2.281000 1.375000 3.656000 ( 3.943304)
这是试运行,但真正体现出像这些线程如何加速比Ruby代码!
请稍候,存在错误('@ lines.each_slice'必须更换'thread_count.times')。 – BernardK
@BernardK,没关系,当你重新后,我会实现你的变化,让你知道如何去。 –
完成。 (以前的版本读取文件'thread_count'次) – BernardK