How to implement method with execution timeout expiration?

Hi,
Would you be so kind to advise me Player#get_action() implementation?
It should return value within timeout regardless of @connection
troubles.

class Player

def get_action( default_action, timeout_sec )
self.within_timeout( timeout_sec ) do
@connection.get_action
end || default_action
end

def get_action timeout_sec &block
# what should be here ???
ned

end

action = Player.new( “Peter” ).get_action :fold, 30

Thank you inadvance!

On Mon, Sep 19, 2011 at 2:03 PM, Sub Z. [email protected] wrote:

end || default_action

Thank you inadvance!


Posted via http://www.ruby-forum.com/.

This sounds more like a design issue, if your system blocks while
@connection is running, you have few choices but create a separate
thread,
and wait for it (or destroy it on timeout).

So my suggestions would either be.

  1. Check if @connection supports some kind of timeout,
  2. Use an event based system where the triggered event is bound to a
    connection and a timeout. The underlying system would either disconnect
    the
    actual request or mark it as invalid if or when an answer arrives.

Good luck!

– John-John T.

On Mon, Sep 19, 2011 at 4:03 PM, Sub Z. [email protected] wrote:

end || default_action

Thank you inadvance!

10:00:52 Temp$ irb19
Ruby version 1.9.2
irb(main):001:0> require ‘timeout’
=> true
irb(main):002:0> timeout(10) { puts 123 }
123
=> nil
irb(main):003:0> timeout(10) { sleep 11; puts 123 }
Timeout::Error: execution expired
from (irb):3:in sleep' from (irb):3:in block in irb_binding’
from (irb):3
from /opt/bin/irb19:12:in `’

Kind regards

robert