[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Timeout Idiom

Chuckl

12/9/2004 3:48:00 PM

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.

3 Answers

Brian Schröder

12/9/2004 4:43:00 PM

0

On Fri, 10 Dec 2004 00:52:27 +0900
"Chuckl" <Killian2422@yahoo.com> wrote:

> 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.
--
Brian Schröder
http://www.brian-sch...



Joel VanderWerf

12/10/2004 12:09:00 AM

0

Chuckl wrote:
> 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....


Chuckl

12/10/2004 10:09:00 AM

0

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.