How to implement signal.wait(timeout) ?

Discussion in 'Ruby' started by Jacob, Sep 30, 2008.

  1. Jacob

    Jacob Guest

    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
     
    Jacob, Sep 30, 2008
    #1
    1. Advertising

  2. Re: How to implement signal.wait(timeout) ?

    On Tue, Sep 30, 2008 at 7:59 PM, Jacob <> 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-without-mercy
    2) http://ph7spot.com/articles/system_timer

    Mikel

    --
    http://lindsaar.net/
    Rails, RSpec and Life blog....
     
    Mikel Lindsaar, Sep 30, 2008
    #2
    1. Advertising

  3. Jacob

    Ron Fox Guest

    Re: How to implement signal.wait(timeout) ?

    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 <> 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-without-mercy
    > 2) http://ph7spot.com/articles/system_timer
    >
    > Mikel
    >



    --
    Ron Fox
    NSCL
    Michigan State University
    East Lansing, MI 48824-1321
     
    Ron Fox, Oct 1, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Peters

    wait for signal change

    Andy Peters, May 17, 2005, in forum: VHDL
    Replies:
    3
    Views:
    4,032
    Klaus Falser
    May 18, 2005
  2. Replies:
    2
    Views:
    1,483
    micky
    Aug 29, 2005
  3. Guest
    Replies:
    1
    Views:
    407
    Victor Bazarov
    Jul 26, 2004
  4. Huey

    How to make "fork/wait" to WAIT longer?

    Huey, Mar 1, 2004, in forum: C Programming
    Replies:
    1
    Views:
    1,968
    -berlin.de
    Mar 1, 2004
  5. Mark Probert

    Timeout::timeout and Socket timeout

    Mark Probert, Oct 6, 2004, in forum: Ruby
    Replies:
    1
    Views:
    1,291
    Brian Candler
    Oct 6, 2004
Loading...

Share This Page