Class: AsyncPlay::QueueWithTimeout

Inherits:
Object
  • Object
show all
Defined in:
lib/async_play.rb

Overview

It is an queue occurs error if there is no result within 1 second. When results of asynchronous function can not be obtained, make an error to prevent waiting indefinitely. See spin.atomicobject.com/2014/07/07/ruby-queue-pop-timeout/

Instance Method Summary collapse

Constructor Details

#initializeQueueWithTimeout

Returns a new instance of QueueWithTimeout.



17
18
19
20
21
# File 'lib/async_play.rb', line 17

def initialize
  @mutex = Mutex.new
  @queue = []
  @recieved = ConditionVariable.new
end

Instance Method Details

#popObject



30
31
32
# File 'lib/async_play.rb', line 30

def pop
  pop_with_timeout 1
end

#push(x) ⇒ Object



23
24
25
26
27
28
# File 'lib/async_play.rb', line 23

def push(x)
  @mutex.synchronize do
    @queue << x
    @recieved.signal
  end
end