Class: Async::LimitedQueue
- Inherits:
-
Queue
- Object
- Condition
- Notification
- Queue
- Async::LimitedQueue
- Defined in:
- lib/async/queue.rb
Instance Attribute Summary collapse
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
Attributes inherited from Queue
Instance Method Summary collapse
- #<<(item) ⇒ Object
- #dequeue ⇒ Object
- #enqueue(*items) ⇒ Object
-
#initialize(limit = 1, **options) ⇒ LimitedQueue
constructor
A new instance of LimitedQueue.
-
#limited? ⇒ Boolean
Whether trying to enqueue an item would block.
Methods inherited from Queue
Methods inherited from Notification
Methods inherited from Condition
Constructor Details
#initialize(limit = 1, **options) ⇒ LimitedQueue
Returns a new instance of LimitedQueue.
79 80 81 82 83 84 85 |
# File 'lib/async/queue.rb', line 79 def initialize(limit = 1, **) super(**) @limit = limit @full = Notification.new end |
Instance Attribute Details
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
87 88 89 |
# File 'lib/async/queue.rb', line 87 def limit @limit end |
Instance Method Details
#<<(item) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/async/queue.rb', line 94 def <<(item) while limited? @full.wait end super end |
#dequeue ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/async/queue.rb', line 115 def dequeue item = super @full.signal return item end |
#enqueue(*items) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/async/queue.rb', line 102 def enqueue *items while !items.empty? while limited? @full.wait end available = @limit - @items.size @items.concat(items.shift(available)) self.signal unless self.empty? end end |
#limited? ⇒ Boolean
Returns Whether trying to enqueue an item would block.
90 91 92 |
# File 'lib/async/queue.rb', line 90 def limited? @items.size >= @limit end |