0.0
No commit activity in last 3 years
No release in over 3 years
Timeout.timeout replacement that uses only 1 thread
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 2.13

Runtime

 Project Readme

frugal_timeout

Build Status Coverage Status Code Climate

Ruby Timeout.timeout replacement using only 1 thread

Why

As you may know, the stock Timeout.timeout uses thread per timeout call. If you use it a lot, you will soon be out of threads. This gem is to provide an alternative that uses only 1 thread.

Also, there's a race condition in the 1.9-2.0 stock timeout. Consider the following code:

timeout(0.02) {
  timeout(0.01, IOError) { sleep }
}

In this case, the stock timeout will most likely raise IOError, but, given the race condition, sometimes it can also raise Timeout::Error. Just put `sleep 0.1' inside stock timeout ensure to trigger that. As of version 0.0.9, frugal_timeout will always raise IOError.

Example

require 'frugal_timeout'

begin
  FrugalTimeout.timeout(0.1) { sleep }
rescue Timeout::Error
  puts 'it works!'
end

# Ensure that calling timeout() will use FrugalTimeout.timeout().
FrugalTimeout.dropin!

# Rescue frugal-specific exception if needed.
begin
  timeout(0.1) { sleep }
rescue FrugalTimeout::Error
  puts 'yay!'
end

Installation

Tested on Ruby 1.9.3 and 2.0.0.

gem install frugal_timeout