平行线
在并行进程(>使用所有CPU)或线程(>加速阻止操作)中运行任何代码。 最适合于地图缩减或例如并行下载/上传。
安装
gem install parallel
用法
# 2 CPUs -> work in 2 processes (a,b + c)
results = Parallel . map ( [ 'a' , 'b' , 'c' ] ) do | one_letter |
expensive_calculation ( one_letter )
end
# 3 Processes -> finished after 1 run
results = Parallel . map ( [ 'a' , 'b' , 'c' ] , in_processes : 3 ) { | one_letter | ... }
# 3 Threads -> finished after 1