tags:

views:

103

answers:

2

I have a library method that occasionally hangs on a network connection, and there's no timeout mechanism.

What's the easiest way to add my own? Basically, I'm trying to keep my code from getting indefinitely stuck.

+4  A: 

Answered my own question:

http://www.ruby-doc.org/stdlib/libdoc/timeout/rdoc/index.html

require 'timeout' status = Timeout::timeout(5) { # Something that should be interrupted if it takes too much time... }

Aaron F.
+2  A: 

timeout.rb has some problems where basically it doesn't always work quite right, and I wouldn't recommend using it. Check System Timer or Terminator instead

The System Timer page in particular describes why timeout.rb can fail, complete with pretty pictures and everything. Bottom line is:

  • For timeout.rb to work, a freshly created “homicidal” Ruby thread has to be scheduled by the Ruby interpreter.
  • M.R.I. 1.8, the interpreter used by most Ruby applications in production, implements Ruby threads as green threads.
  • It is a well-known limitations of the green threads (running on top of a single native thread) that when a green thread performs a blocking system call to the underlying operating systems, none of green threads in the virtual machine will run until the system call returns.
AdminMyServer