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!
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’
===============================================================================
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.
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
–
ATTN: please update your address books with address below!
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’
===============================================================================