Class: Lowdown::Threading::DispatchQueue
- Inherits:
-
Object
- Object
- Lowdown::Threading::DispatchQueue
- Defined in:
- lib/lowdown/threading.rb
Instance Method Summary collapse
- #dispatch(&block) ⇒ Object
-
#drain!(rescue_exceptions = (Thread.current != Thread.main)) ⇒ Object
Performs the number of dispatched blocks that were on the queue at the moment of calling #drain!.
- #empty? ⇒ Boolean
-
#initialize ⇒ DispatchQueue
constructor
A new instance of DispatchQueue.
Constructor Details
#initialize ⇒ DispatchQueue
Returns a new instance of DispatchQueue.
6 7 8 |
# File 'lib/lowdown/threading.rb', line 6 def initialize @queue = Queue.new end |
Instance Method Details
#dispatch(&block) ⇒ Object
10 11 12 |
# File 'lib/lowdown/threading.rb', line 10 def dispatch(&block) @queue << block end |
#drain!(rescue_exceptions = (Thread.current != Thread.main)) ⇒ Object
Performs the number of dispatched blocks that were on the queue at the moment of calling #drain!. Unlike performing blocks _until the queue is empty_, this ensures that it doesn’t block the calling thread too long if another thread is dispatching more work at the same time.
By default this will let any exceptions bubble up on the main thread or catch and return them on other threads.
24 25 26 27 28 29 30 |
# File 'lib/lowdown/threading.rb', line 24 def drain!(rescue_exceptions = (Thread.current != Thread.main)) @queue.size.times { @queue.pop.call } nil rescue Exception => exception raise unless rescue_exceptions exception end |
#empty? ⇒ Boolean
14 15 16 |
# File 'lib/lowdown/threading.rb', line 14 def empty? @queue.empty? end |