Class: HandlingQueue
- Inherits:
-
Object
- Object
- HandlingQueue
- Defined in:
- lib/handling_queue.rb,
lib/handling_queue/request.rb,
lib/handling_queue/version.rb
Overview
Execute handling process from time to time.
Defined Under Namespace
Classes: Request
Constant Summary collapse
- VERSION =
Library version.
'1.1.0'
Instance Method Summary collapse
-
#handle(obj) ⇒ void
Request handling of
objand wait for it to be handled. -
#initialize(slice: 1, interval: 1) {|obj| ... } ⇒ HandlingQueue
constructor
A new instance of HandlingQueue.
Constructor Details
#initialize(slice: 1, interval: 1) {|obj| ... } ⇒ HandlingQueue
Returns a new instance of HandlingQueue.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/handling_queue.rb', line 11 def initialize(slice: 1, interval: 1, &handler) @slice = slice @interval = interval @handler = handler @requests_queue = [] # Use Array with #push and #shift methods to have queue. # NOTE: effeciency is pretty good, btw. O(1) if no new memory required. @mutex = Mutex.new # Queue and requests accessing mutex @cv = ConditionVariable.new # Condition variable to signal when some of results are ready. @working = false @thread = nil start_handler_thread end |
Instance Method Details
#handle(obj) ⇒ void
This method returns an undefined value.
Request handling of obj and wait for it to be handled.
30 31 32 33 34 35 36 37 |
# File 'lib/handling_queue.rb', line 30 def handle(obj) request = Request.new(obj) @mutex.synchronize do @requests_queue.push(request) @cv.wait(@mutex) until request.re? request.re end end |