tags:

views:

54

answers:

1

I found ruby class Timeout very useful for my project.

But i need to run a block of code in background and keep it under a timeout..

For example

Timeout::timeout(2) { block.call }

How to do that?

A: 
require 'timeout'

t = Thread.new {
  Timeout.timeout(5) {
    while(true) do
      puts 'a'
      sleep 1
    end
  }
}
t.join

However, Timeout will throw an exception upon expiration, but I'm guess you're ok with it.

Mladen Jablanović