【问题标题】:Are the variables scoped in threads变量是否在线程中作用域
【发布时间】:2013-10-09 22:19:57
【问题描述】:

我正在尝试从网站上抓取一些信息,但我以前从未使用过线程。我将这个测试拼凑在一起以模仿我正在尝试做的事情:

require 'thread'
mutex = Mutex.new
mut = Mutex.new
hash = {}
n = 0
a = []
b = []
# x = 0
10.times do |i|
 a << Thread.new(i) do |top_index|
   mutex.synchronize do
     hash[top_index] = []
     sleep 0.2
     100.times do |sub_index|
       b << Thread.new(top_index, sub_index, hash) do |t, s, my_hash|
         mut.synchronize do
           r = s
           sleep 0.2
           my_hash[t].push(s)
         end
       end
     end
     b.each {|y| y.join }
     puts "sub: #{top_index} - #{hash[top_index].length}"
     puts hash[top_index]
   end
 end
end
a.each {|q| q.join }
hash.each { |key, value| n += value.length }
puts "Final Tally - #{n}"

sleep 代表一些 RestClient get 请求,以及代表我从网站上抓取的一些信息的排序和 pushing 的数字。但是,当查看输入所有内容的顺序时,我注意到数组之间的模式,所以我想知道当r 在一个线程中分配时是否会影响它在另一个线程中的值。但这没有意义,因为这会严重限制它对并发请求的有用性。

另外,我认为由于一切都是并发的(或表现得像并发的),它应该在几秒钟内通过睡眠计时器返回,但实际上需要相当长的时间。

我刚刚测试了一下,居然比没有线程的时间长?

线程总时间:204.04028

正常总数:203.133638

所以,现在我很困惑。

【问题讨论】:

  • 一个线程,其整个操作都包装在一个互斥体中?
  • 这很糟糕吗?我没有找到太多关于嵌套线程的内容,但我也没有看到有人说不要这样做。如果没有它,它就真的无法工作。
  • 也许您可以解释一下“没有它就无法工作”是什么意思。如果事实证明您确实需要同步部分工作,请隔离该部分并仅同步尽可能少的代码。

标签: ruby multithreading


【解决方案1】:

我不知道您注意到什么“模式”;但一般来说,您在示例中使用 Thread 初始化程序的方式应该可以按预期工作。

我刚刚测试了一下,居然比没有线程的时间长?

这是因为您正在同步使用这些线程进行的所有工作。所以并发性为零。因此,单线程解决方案优于“多线程”解决方案是有道理的,因为后者只是做与前者相同的工作(以相同的顺序),但产生线程的额外开销(并使它们等等)。

您不需要同步这些操作。 Ruby 解释器有一个global interpreter lock,它可以防止开发人员在低级语言中遇到的大多数竞争条件。您希望使用Mutex 的主要场景是当可能发生需要同步的外部 Ruby 领域(例如,一些较低级别的系统操作)时。

这是您的示例的精简版本(没有同步),效果很好:

require 'thread'

hash = {}
outer_threads = []
inner_threads = []

10.times do |i|
 outer_threads << Thread.new(i) do |top_index|
   hash[top_index] = []
   sleep 0.2
   20.times do |sub_index|
     inner_threads << Thread.new(top_index, sub_index, hash[top_index]) do |t, s, arr|
       sleep 0.2
       arr.push(s + 1)
     end
   end
   inner_threads.each(&:join)
 end
end

outer_threads.each(&:join)

# Verify that the hash is populated with arrays comprising the numbers 1 to 20,
# as we would expect.
hash.each do |key, value|
  puts "#{key}: #{value.sort.join(', ')}"
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-15
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多