Path:
timeout.rb
Last update:
Tue May 25 13:04:11 -0700 2010
Ruby version:
Ruby 1.9.2
execution timeout
require 'timeout' status = Timeout::timeout(5) { # Something that should be interrupted if it takes too much time... }
A way of performing a potentially long-running operation in a thread, and terminating it‘s execution if it hasn‘t finished by a fixed amount of time.
Previous versions of timeout didn‘t provide use a module for namespace. This version provides both Timeout.timeout, and a backwards-compatible timeout.
Copyright:
(C) 2000 Network Applied Communication Laboratory, Inc.
Copyright:
(C) 2000 Information-technology Promotion Agency, Japan
TimeoutError
=
Timeout::Error
Another name for Timeout::Error, defined for backwards compatibility with earlier versions of timeout.rb.
Identical to:
Timeout::timeout(n, e, &block).
Defined for backwards compatibility with earlier versions of timeout.rb, see Timeout#timeout.
# File timeout.rb, line 86 def timeout(n, e = nil, &block) Timeout::timeout(n, e, &block) end
Ruby-doc.org is hosted by James Britt and Neurogami, an avant-garage application development company in Scottsdale, AZ.
Documentation content on ruby-doc.org is provided by remarkable members of the Ruby community.
For more information on the Ruby programming language, visit ruby-lang.org.
Want to help improve Ruby's API docs? See Ruby Documentation Guidelines.
Rolling on Rails
Just a small tip, if you wish to ensure a snippet of Ruby code doesn’t run for too long you can use the timeout function. You might want to do this when making a request to a remote server with net/http
for example.
A way of performing a potentially long-running operation in a thread, and terminating it‘s execution if it hasn‘t finished within fixed amount of time.
Here’s a quick example using the excellent rFeedParser (Universal Feed Parser in Ruby) to fetch an RSS feed.
require 'timeout' require 'zlib' require 'rubygems' require 'rfeedparser' fp = nil begin # Don't take longer than 20 seconds to retrieve & parse an RSS feed Timeout::timeout(20) do fp = FeedParser.parse("http://feeds.feedburner.com/slashdotdash") end rescue Timeout::Error # Too slow!! end
You’re currently reading “Ruby Tidbit: Timeout code execution,” an entry on Slash Dot Dash
timeout is unsafe.
See http://headius.blogspot.com/2008/02/rubys-threadraise-threadkill-timeoutrb.html
Welcome to the blog of Ben Smith a Ruby on Rails aficionado.