【发布时间】:2017-07-04 08:17:23
【问题描述】:
您介意告诉我我在 Ruby 中的线程是否正确吗?
我有一个数组items,我想在 16 个线程中处理它,等到所有线程都完成后,再做更多的工作。
这是我的代码;
chunks = items.each_slice(items.count / 16).to_a
puts chunks.length
queue = Queue.new
semaphore = Mutex.new
threads = []
puts '[+] Building Queue'
chunks.each do |chunk|
threads << Thread.new {
chunk.each do |item|
array = []
metadata.each do |m|
array << m.evaluate(item) rescue ''
end
semaphore.synchronize {
queue << array.join(20.chr)
}
end
}
end
puts '[+] Running threads'
threads.each{|t|t.join; puts 'Threads done'}
#Do more work here, queue should be fully populated
puts '[+] Writing to file'
File.open('E:/Export.dat', 'wt', encoding: 'UTF-16LE') do |f|
until queue.empty?
unit = queue.pop(true) rescue nil
if unit
f.puts unit
end
end
end
代码运行,但没有给我预期的线程性能,对吗?
谢谢
【问题讨论】:
-
您的 ruby 解释器是否允许在多个线程上运行?
-
是的,我只是想确定我的代码逻辑是否合理
-
您有多少 CPU 可用?
-
逻辑没问题。这就是我问这个问题的原因。应该更快。
-
你在哪里使用
queue?
标签: ruby multithreading jruby