【问题标题】:Ruby multiple background threadsRuby 多个后台线程
【发布时间】:2010-05-20 01:10:03
【问题描述】:

我需要在超时的线程池中运行多个后台线程。 该方案类似于:

    #!/usr/bin/env ruby

require 'thread'

def foo(&block)
  bar(block)
end

def bar(block)
  Thread.abort_on_exception=true
  @main = Thread.new { block.call }
end


foo {
sleep 1
puts 'test'
}

为什么如果我运行我没有输出? (并且没有睡眠等待?)

【问题讨论】:

    标签: ruby


    【解决方案1】:

    程序在主线程结束时结束。你必须等待bar使用join创建的线程:

    foo {
      sleep 1
      puts 'test'
    }.join
    

    【讨论】:

      【解决方案2】:

      试试 work_queue gem http://rubygems.org/gems/work_queue/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-18
        • 1970-01-01
        • 2014-09-27
        相关资源
        最近更新 更多