timeout w/o timeout.rb

A

Ara.T.Howard

i'm writing some unit tests which do alot of io/select/etc - bad things to mix
with a thread based 'timeout' method... i've been using this instead:

# a timeout method which does not fail when io blocks
trap 'SIGALRM', 'IGNORE'
class TimeoutError < StandardError; end
def timeout n
begin
trap('SIGALRM'){throw :timeout, TimeoutError.new}
cid = nil
unless((cid = fork))
sleep n
Process.kill 'SIGALRM', Process.ppid
else
thrown =
catch:)timeout) do
ret = yield
Process.kill 'SIGKILL', cid
ret
end
raise thrown if TimeoutError === thrown
end
ensure
trap 'SIGALRM', 'IGNORE'
end
end

any massive problems with this (this impl is a mega hack i realize)?

does it seem like some permutation of this might be useful? something like
Process#timeout perhaps??

-a
--

ATTN: please update your address books with address below!

===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| STP :: http://www.ngdc.noaa.gov/stp/
| NGDC :: http://www.ngdc.noaa.gov/
| NESDIS :: http://www.nesdis.noaa.gov/
| NOAA :: http://www.noaa.gov/
| US DOC :: http://www.commerce.gov/
|
| The difference between art and science is that science is what we
| understand well enough to explain to a computer.
| Art is everything else.
| -- Donald Knuth, "Discover"
|
| /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done'
===============================================================================
 
P

Pit Capitain

Ara.T.Howard said:
any massive problems with this (this impl is a mega hack i realize)?

Doesn't work on windows (this is a problem with me, not with your code :) )

Regards,
Pit
 
J

Jamis Buck

Ara.T.Howard said:
i'm writing some unit tests which do alot of io/select/etc - bad things to mix
with a thread based 'timeout' method... i've been using this instead:

# a timeout method which does not fail when io blocks
trap 'SIGALRM', 'IGNORE'
class TimeoutError < StandardError; end
def timeout n
begin
trap('SIGALRM'){throw :timeout, TimeoutError.new}
cid = nil
unless((cid = fork))
sleep n
Process.kill 'SIGALRM', Process.ppid
else
thrown =
catch:)timeout) do
ret = yield
Process.kill 'SIGKILL', cid
ret
end
raise thrown if TimeoutError === thrown
end
ensure
trap 'SIGALRM', 'IGNORE'
end
end

any massive problems with this (this impl is a mega hack i realize)?

does it seem like some permutation of this might be useful? something like
Process#timeout perhaps??

-a

Doesn't Ruby use SIGALRM internally? I could be wrong, but if it does,
you may be asking for trouble by using it in your script.

- Jamis

--
Jamis Buck
(e-mail address removed)
http://www.jamisbuck.org/blog/jamis.cgi

ruby -h | ruby -e 'a=[];readlines.join.scan(/-(.)\[e|Kk(\S*)|le.l(..)e|#!(\S*)/) {|r| a << r.compact.first };puts "\n>#{a.join(%q/ /)}<\n\n"'
 
A

Ara.T.Howard

Date: Tue, 10 Feb 2004 09:32:11 +0900
From: Jamis Buck <[email protected]>
Newsgroups: comp.lang.ruby
Subject: Re: timeout w/o timeout.rb



Doesn't Ruby use SIGALRM internally? I could be wrong, but if it does,
you may be asking for trouble by using it in your script.

- Jamis

ah - good point - and SIGUSR1 would work just as well

-a
--

ATTN: please update your address books with address below!

===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| STP :: http://www.ngdc.noaa.gov/stp/
| NGDC :: http://www.ngdc.noaa.gov/
| NESDIS :: http://www.nesdis.noaa.gov/
| NOAA :: http://www.noaa.gov/
| US DOC :: http://www.commerce.gov/
|
| The difference between art and science is that science is what we
| understand well enough to explain to a computer.
| Art is everything else.
| -- Donald Knuth, "Discover"
|
| /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done'
===============================================================================
 

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

Similar Threads

tk file dialog and directories 3
puts(derived_from_array) 0
io/nonblock 4
make MISSING=flock.o 0
non-blocking io 2
[RCR] Kernel#hostname 0
RCR - 'struct flock*' wrapper for rb_io_fcntl 7
pp equiv of #inspect 1

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,743
Latest member
WoodrowMea

Latest Threads

Top