SystemTimer 1.2 Release

I have just published SystemTimer 1.2 release on Gemcutter. This new version provides support for custom timeout exceptions, will let you specify sub-second timeouts and plays nicer with Ruby interpreters compiled with -disable-pthreads.

Install SystemTimer latest version with:

sudo gem install SystemTimer

Support for Custom Timeout Exceptions

This version adds support for custom timeout exceptions. This is useful when you want to avoid interference with other libraries already using/catching Timeout::Error (e.g. Net::HTTP)

require 'system_timer'

begin
  SystemTimer.timeout_after(5, MyCustomTimeoutException) do
    # Something that should be interrupted if it 
    # takes too much time...  even if blocked on 
    # a system call!
  end
rescue MyCustomTimeoutException => e
  # Recovering strategy
end

This patch was kindly contributed by runix

Sub-second Timeouts

SystemTimer is going through too many layers to be able to reliably guarantee a sub-second timeout on all platforms, so – in the original SystemTimer implementation – the timeout had to be expressed as a number of seconds.

You can now specify timeouts as a fraction of a second and SystemTimer will do its best to reduce the timeout accordingly. e.g.

SystemTimer.timeout_after(0.5) do 
  # timeout after 500ms
end

Note that for stability reasons SystemTimer will not allow you to go below 200ms, e.g.

SystemTimer.timeout_after(0.01) do 
  # timeout at best after (uncompressable) 200ms 
  # even if 10ms is requested
end

This feature is based on an idea and original contribution by Dmytro Shteflyuk (of Scribd fame).

Better Compatibility with -disable-pthreads

Changed SystemTimer implementation from using Mutex to Monitor. Mutex causes thread join errors when Ruby is compiled with -disable-pthreads.

Thanks again to Dmytro Shteflyuk who contributed this patch.

What Next?

Talk back

Post the first comment to share the love or get a discussion going!

Bookmark it

You can bookmark this document directly or by a simple click to Digg, del.icio.us or Reddit.

Recommend me

If you have enjoyed this article, you might consider recommending me on Working With Rails.

Subscribe to RSS

If you're familiar with RSS, you might want to subscribe to the PH7 RSS feed. You can use one-click subscriptions to our RSS-feed through Bloglines, Google Reader, My Yahoo, Newsgator, Rojo

This work is licensed under a Creative Commons Attribution 2.5 License.

Original web site design by: JFX diz*web.