浙江省高等学校教师教育理论培训

微信搜索“毛凌志岗前心得”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

timeout

Constants

TimeoutError
=
Timeout::Error
Another name for Timeout::Error, defined for backwards compatibility with earlier versions of timeout.rb.

Public Instance methods

timeout(n, e = nil, &block)

Identical to:

  Timeout::timeout(n, e, &block).

Defined for backwards compatibility with earlier versions of timeout.rb, see Timeout#timeout.

[Source]

# 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.

timeout.rb

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.
  1. 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

Working With Rails

Recently

Categories


Powered by Hemingway flavored Wordpress.

Entries RSS Comments RSS

posted on 2011-01-14 09:51  lexus  阅读(343)  评论(0编辑  收藏  举报