Timeout Idiom

C

Chuckl

This idiom has been a decent shortcut for my work:

require 'timeout'


def timeout?(sec)
begin
freq = [0.01, sec / 1000].max
timeout(sec) {
while true
return false if yield
sleep(freq)
end
}
rescue Timeout::Error
return true
end
end


Code police may even approve, since 'timeout.rb' kinda uses
exception-throwing as flow control.
 
B

Brian Schröder

This idiom has been a decent shortcut for my work:

require 'timeout'


def timeout?(sec)
begin
freq = [0.01, sec / 1000].max
timeout(sec) {
while true
return false if yield
sleep(freq)
end
}
rescue Timeout::Error
return true
end
end


Code police may even approve, since 'timeout.rb' kinda uses
exception-throwing as flow control.

Some little style improvements. Note that you automatically have a codeblock in
a function, so you can spare the extra begin - end. Also there is a loop
function that makes while(true) explicit.

A personal style thing is, that I like to use { } for blocks whose return value
is of interest, and do end blocks for blocks whose side-effects are of
interest.


require 'timeout'

def timeout?(sec)
freq = [0.01, sec / 1000].max
timeout(sec) do
loop do
return false if yield
sleep(freq)
end
end
rescue Timeout::Error
return true
end

[0.5, 0.9, 1.0, 2.0, 3.0].each do | sleep_time |
result = timeout?(1) do sleep sleep_time end
puts "While sleeping #{sleep_time}s #{result ? 'a' : 'no'} timeout occured."
end


Best regards,

Brian

PS: Unindented code is really hard to read. I only understood your code after
using xemacs autoindentation.
 
J

Joel VanderWerf

Chuckl said:
This idiom has been a decent shortcut for my work:

require 'timeout'


def timeout?(sec)
begin
freq = [0.01, sec / 1000].max
timeout(sec) {
while true
return false if yield
sleep(freq)
end
}
rescue Timeout::Error
return true
end
end


Code police may even approve, since 'timeout.rb' kinda uses
exception-throwing as flow control.

Here's a class I've found useful (I use a C version, as well). It avoids
the drift problem of just calling sleep(constant) for a constant value
each time--it measures the error and corrects for it, if possible, so
that even it will always try to get back on track. No threads,
exceptions, or throw. Both external and internal iterator forms. Tests
and docs included.

http://redshift.sourceforge.net/timer/
 
C

Chuckl

Sorry about the (lack of) indentation. That's what I get for using
Google groups and skipping "preview." And thanks for the style advice
-- obviously I'm new to Ruby.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top