Ruby multiple background threads

I need to start multiple background threads in a thread pool with a timeout. The diagram looks something like this:

    #!/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, I don't get the exit? (and no waiting waiting?)

+2


a source to share


2 answers


The program ends when the main thread ends. You have to wait on the thread created bar

with join

:



foo {
  sleep 1
  puts 'test'
}.join

      

+3


a source


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



+3


a source







All Articles