开发者

Ruby multiple background threads

开发者 https://www.devze.com 2022-12-30 16:15 出处:网络
I need t开发者_StackOverflow社区o run multiple background threads in a thread pool with timeout.

I need t开发者_StackOverflow社区o run multiple background threads in a thread pool with timeout. The scheme is something like:

    #!/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'
}

Why if i run that i get no output? (and no sleep wait?)


The program ends when the main thread ends. You have to wait on the thread created by bar using join:

foo {
  sleep 1
  puts 'test'
}.join


Try the work_queue gem http://rubygems.org/gems/work_queue/

0

精彩评论

暂无评论...
验证码 换一张
取 消