timeout.rb
Path:
timeout.rb
Last update:
Tue May 25 13:04:11 -0700 2010
Ruby version:
Ruby 1.9.2
timeout.rb
execution timeout
Synopsis
require 'timeout' status = Timeout::timeout(5) { # Something that should be interrupted if it takes too much time... }
Description
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
Copyright:
(C) 2000 Network Applied Communication Laboratory, Inc.
Copyright:
(C) 2000 Information-technology Promotion Agency, Japan
Methods
Constants
TimeoutError
=
Timeout::Error
Another name for Timeout::Error, defined for backwards compatibility with earlier versions of timeout.rb.
Public Instance methods
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.
Slash Dot Dash
Rolling on Rails
Ruby Tidbit: Timeout code execution
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
About this entry
You’re currently reading “Ruby Tidbit: Timeout code execution,” an entry on Slash Dot Dash
- Published:
- 02.15.08 / 12am
- Category:
- Ruby, Ruby on Rails
Comments are closed
Comments are currently closed on this entry.- David Roussel 02.29.08 / 2pm
timeout is unsafe.
See http://headius.blogspot.com/2008/02/rubys-threadraise-threadkill-timeoutrb.html
About
Welcome to the blog of Ben Smith a Ruby on Rails aficionado.
Top Posts
Recently
- 09.27 Alistair Cockburn’s lightning talk on CQRS recorded at the Mountain West Ruby Conference 2010
- 09.27 Rails 3, RSpec and Cucumber setup
- 09.25 Rails 3 scopes with chaining
- 03.18 Ruby and Rails Magazines available to Buy or download
- 01.02 Migrating this blog from Typo to WordPress
- 09.20 Easy Merb installation using Thor
- 07.05 Getting started with Merb and Datamapper
- 03.12 Lifestreaming – combining your online activity into a single stream (using trawlr)
- 03.09 Sphinx search with delta indexing thanks to Ultrasphinx
- 03.08 Pimp my MacBook Pro
Categories
- .NET (3)
- Amusing (4)
- Apple (12)
- Blog (5)
- CQRS (1)
- Facebook (1)
- iPhone (4)
- Linux (4)
- Mac OS X (17)
- Merb (2)
- Ruby (2)
- Ruby on Rails (57)
- Typo (4)
- Uncategorized (15)
- Windows (1)