In Files

Parent

EventMachine::Queue

A cross thread, reactor scheduled, linear queue.

This class provides a simple “Queue” like abstraction on top of the reactor scheduler. It services two primary purposes:

See examples/ex_queue.rb for a detailed example.

 q = EM::Queue.new
 q.push('one', 'two', 'three')
 3.times do
   q.pop{ |msg| puts(msg) }
 end

Public Class Methods

new() click to toggle source

Create a new queue

    # File lib/em/queue.rb, line 19
19:     def initialize
20:       @items = []
21:       @popq  = []
22:     end

Public Instance Methods

empty?() click to toggle source

N.B. This is a peek, it’s not thread safe, and may only tend toward accuracy.

    # File lib/em/queue.rb, line 51
51:     def empty?
52:       @items.empty?
53:     end
pop(*a, &b) click to toggle source

Pop items off the queue, running the block on the reactor thread. The pop will not happen immediately, but at some point in the future, either in the next tick, if the queue has data, or when the queue is populated.

    # File lib/em/queue.rb, line 27
27:     def pop(*a, &b)
28:       cb = EM::Callback(*a, &b)
29:       EM.schedule do
30:         if @items.empty?
31:           @popq << cb
32:         else
33:           cb.call @items.shift
34:         end
35:       end
36:       nil # Always returns nil
37:     end
push(*items) click to toggle source

Push items onto the queue in the reactor thread. The items will not appear in the queue immediately, but will be scheduled for addition during the next reactor tick.

    # File lib/em/queue.rb, line 42
42:     def push(*items)
43:       EM.schedule do
44:         @items.push(*items)
45:         @popq.shift.call @items.shift until @items.empty? || @popq.empty?
46:       end
47:     end
size() click to toggle source

N.B. This is a peek, it’s not thread safe, and may only tend toward accuracy.

    # File lib/em/queue.rb, line 57
57:     def size
58:       @items.size
59:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.