E
Eric Schwartz
Following advice in an old ruby-talk thread (can't remember which one,
offhand), I'm trying to implement a timeout with threads. The
canonical example was something like:
Thread.new do
sleep 5
Process.kill "ALRM", $$
end
begin
.... some stuff ...
rescue SignalException => se
return FAIL_CODE
end
Well, the problem with that is that the thread keeps executing, and if
the process as a whole takes more than 5 seconds to complete, then the
SIGALRM kills the process.
So fine, thinks I, I'll just stop the thread when I'm done with it.
No problem. Only I can't figure out how. I can't call Thread#stop
from outside the thread. What I finally ended up with was something
like:
rv = nil
Thread.new do
sleep 5
Process.kill "ALRM", $$ if rv == nil
end
..... catch SIGALRM if it happens ...
rv = query_some_stuff
....
return rv
I'm not excruciatingly happy about this solution, and I thought I'd
open it up to the Ruby community: What's the best way to do the
equivalent of 'alarm(5)' in C?
-=Eric
offhand), I'm trying to implement a timeout with threads. The
canonical example was something like:
Thread.new do
sleep 5
Process.kill "ALRM", $$
end
begin
.... some stuff ...
rescue SignalException => se
return FAIL_CODE
end
Well, the problem with that is that the thread keeps executing, and if
the process as a whole takes more than 5 seconds to complete, then the
SIGALRM kills the process.
So fine, thinks I, I'll just stop the thread when I'm done with it.
No problem. Only I can't figure out how. I can't call Thread#stop
from outside the thread. What I finally ended up with was something
like:
rv = nil
Thread.new do
sleep 5
Process.kill "ALRM", $$ if rv == nil
end
..... catch SIGALRM if it happens ...
rv = query_some_stuff
....
return rv
I'm not excruciatingly happy about this solution, and I thought I'd
open it up to the Ruby community: What's the best way to do the
equivalent of 'alarm(5)' in C?
-=Eric