多进程Ruby和存储变量

时间:2017-04-06 23:09:15

标签: ruby

我想知道如何在ruby中多线程并将子进程的结果存储在父数组中。

我想要处理的数组有400个项目。

我尝试了2种解决方案。

Solution: 1 (http://www.rubydoc.info/gems/parallel-forkmanager/Parallel/ForkManager)

require forkmanager

eng = []

array = [{'ip' => 12.34.56,'hostname' => ''}, {'ip' => 22.22.22, 'hostname' => ''}

pm = Parallel::ForkManager.new(20)
pm.run_on_finish{|pid, exit_code, return_list|
  eng << return_list
}

array.each do |node|
  pm.start(node) and wait
  new_node = process(node) #gives a value to hostname 
  pm.finish(0, new_node)
end
pm.wait_all_children
puts eng #expects it to return new node but it returns node. 




Solution 2: (https://github.com/grosser/parallel)
require 'parallel'
eng =[]
array = [{'ip' => 12.34.56,'hostname' => ''}, {'ip' => 22.22.22, 'hostname' => ''}
Parallel.map(array, in_processes: 20) do |node|
  new_node = process(node) #gives a value to hostname 
  eng << new_node
end
puts eng #expected to provide nodes. returns empty array instead.

1 个答案:

答案 0 :(得分:0)

Parallel.mapEnumerable.map非常相似,因为它返回运行数组中给定元素上每个块的结果,因此您可以简单地执行类似这样的操作来获取每个块的处理结果节点:

eng = Parallel.map(array, in_processes: 20) do |node|
  process(node) # Return the result of processing `node`. `Parallel.map` will return these results for each node in an array
end

puts eng # This will have the expected nodes now