当每个元素都需要处理时,我有一个ruby脚本数组:
threads = []
elemets.each do |element|
threads.push(Thread.new{process(element)}}
end
threads.each { |aThread| aThread.join }
然而,由于资源的限制,如果一次处理的四个元素不超过这四个元素,脚本将以最佳方式工作。
不,我知道我可以转储each循环,使用一个变量来计算4个元素,然后等待,但是有没有更酷的ruby方法呢?
发布于 2010-01-01 18:50:26
您可以针对一个数组以4个为一组进行枚举:
>> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].each_slice(4) {|a| p a}
[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]
所以你可以尝试像这样的东西
elements.each_slice(4) do | batch |
batch.each do | element |
threads.push(Thread.new{process(element)}}
end
(do stuff to check to see if the threads are done, otherwise wait )
end
不过,这可能不是你需要的--我从凌晨3点就起床了,只睡了几个小时。:/
发布于 2010-01-01 19:10:35
如果我没看错的话,您希望一次处理的线程不超过4个。
在我看来,您应该只启动4个线程,并让它们都从共享队列(标准线程库的一部分)中读取,以处理这些元素。
您可以让线程在队列为空时结束。
将数组分成4个相等的数组,并让每个线程处理1/4的元素,假设每个元素在相同的时间内处理。如果某些线程花费的时间比其他线程长,那么您的一些线程将提前完成。
使用队列,没有线程停止,直到共享队列为空,所以我认为这是一个更有效的解决方案。
以下是一个基于您的代码的工作程序,用于演示:
require 'thread'
elements = [1,2,3,4,5,6,7,8,9,10]
def process(element)
puts "working on #{element}"
sleep rand * 10
end
queue = Queue.new
elements.each{|e| queue << e }
threads = []
4.times do
threads << Thread.new do
while (e = queue.pop(true) rescue nil)
process(e)
end
end
end
threads.each {|t| t.join }
发布于 2010-01-01 18:27:39
我不确定下面的变种是仅仅使用一个“变量来计算4个元素”,还是可以被认为很酷,但是它给了你一个大小不超过4个元素的切片数组:
x = (1..10).to_a
0.step(x.size - 1, 4) do |i|
# Choose one
p x.slice(i, 4)
p x[i, 4]
end
https://stackoverflow.com/questions/1988274
复制相似问题