From: Joel VanderWerf Date: 2004-12-10T09:08:34+09:00 Subject: Re: Timeout Idiom 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.net/timer/