[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How to implement signal.wait(timeout) ?

Jacob

9/30/2008 9:56:00 AM

Hi all

I am writing a ruby server. Something like:

class MyServer

def initialize
@stop_signal = MySignal.new
end

def start
@stop_signal.reset
@thread = Thread.start { run }
end

def stop
@stop_signal.set
@thread.wait
end

def run

while true
do_process if there_is_some_thing_to_process
@stop_signal.wait 5
end

end

end

Is there any class in ruby that has similar functions to MySignal?
It's easy solution in java or .net.

I have googled, and I found a method call 'timeout', but after read
the source code, I found it uses a new thread to handle timeout. I
think It's not good solution, because on my real system (ported
from .net source code), there are some thread running parallel, and
there are using alot of signal.

Thanks
Jacob
2 Answers

Mikel Lindsaar

9/30/2008 11:40:00 AM

0

On Tue, Sep 30, 2008 at 7:59 PM, Jacob <ngocphuc@gmail.com> wrote:
> I have googled, and I found a method call 'timeout', but after read
> the source code, I found it uses a new thread to handle timeout. I
> think It's not good solution, because on my real system (ported
> from .net source code), there are some thread running parallel, and
> there are using alot of signal.

You will have to code carefully because ruby uses green threads, not
native threads so you can get blocked on system calls.

I had the same sort of problem, in the end, we solved it with the
terminator gem (1)

See the system timer (2) write up for a good explanation of what is happening.

1) http://www.lindsaar.net/2008/9/11/terminator-timeout-wit...
2) http://ph7spot.com/articles/sy...

Mikel

--
http://lin...
Rails, RSpec and Life blog....

Ron Fox

10/1/2008 11:08:00 AM

0

Also see the ALARM signal which allows a signal to be delivered after
some time has elapsed.


Mikel Lindsaar wrote:
> On Tue, Sep 30, 2008 at 7:59 PM, Jacob <ngocphuc@gmail.com> wrote:
>> I have googled, and I found a method call 'timeout', but after read
>> the source code, I found it uses a new thread to handle timeout. I
>> think It's not good solution, because on my real system (ported
>> from .net source code), there are some thread running parallel, and
>> there are using alot of signal.
>
> You will have to code carefully because ruby uses green threads, not
> native threads so you can get blocked on system calls.
>
> I had the same sort of problem, in the end, we solved it with the
> terminator gem (1)
>
> See the system timer (2) write up for a good explanation of what is happening.
>
> 1) http://www.lindsaar.net/2008/9/11/terminator-timeout-wit...
> 2) http://ph7spot.com/articles/sy...
>
> Mikel
>


--
Ron Fox
NSCL
Michigan State University
East Lansing, MI 48824-1321