Class: EventMachine::Queue

### Home Classes Methods

### In Files

- lib/em/queue.rb

### Parent

Object

### Methods

- ::new
- #<<
- #empty?
- #num_waiting
- #pop
- #push
- #size

### Files

- ChangeLog
- DEFERRABLES
- EPOLL
- INSTALL
- KEYBOARD
- LEGAL
- LIGHTWEIGHT_CONCURRENCY
- PURE_RUBY
- RELEASE_NOTES
- SMTP
- SPAWNED_PROCESSES
- TODO

### Class/Module Index

Quicksearch

- EventMachine
- EventMachine
- EventMachine::Channel
- EventMachine::Completion
- EventMachine::Connection
- EventMachine::DNS
- EventMachine::DNS::Request
- EventMachine::DNS::RequestIdAlreadyUsed
- EventMachine::DNS::Resolver
- EventMachine::DNS::Socket
- EventMachine::DatagramObject
- EventMachine::DefaultDeferrable
- EventMachine::Deferrable
- EventMachine::DeferrableChildProcess
- EventMachine::Error
- EventMachine::EvmaKeyboard
- EventMachine::EvmaTCPClient
- EventMachine::EvmaTCPServer
- EventMachine::EvmaUDPSocket
- EventMachine::EvmaUNIXClient
- EventMachine::EvmaUNIXServer
- EventMachine::FileNotFoundException
- EventMachine::FileStreamer
- EventMachine::FileWatch
- EventMachine::Iterator
- EventMachine::LoopbreakReader
- EventMachine::PeriodicTimer
- EventMachine::Pool
- EventMachine::ProcessWatch
- EventMachine::Protocols
- EventMachine::Protocols
- EventMachine::Protocols::HeaderAndContentProtocol
- EventMachine::Protocols::HttpClient
- EventMachine::Protocols::HttpClient2
- EventMachine::Protocols::HttpClient2::Request
- EventMachine::Protocols::LineAndTextProtocol
- EventMachine::Protocols::LineProtocol
- EventMachine::Protocols::LineText2
- EventMachine::Protocols::Memcache
- EventMachine::Protocols::Memcache::ParserError
- EventMachine::Protocols::ObjectProtocol
- EventMachine::Protocols::Postgres3
- EventMachine::Protocols::SASLauth
- EventMachine::Protocols::SASLauthclient
- EventMachine::Protocols::SmtpClient
- EventMachine::Protocols::SmtpServer
- EventMachine::Protocols::Socks4
- EventMachine::Protocols::Stomp
- EventMachine::Protocols::Stomp::Message
- EventMachine::Protocols::TcpConnectTester
- EventMachine::Queue
- EventMachine::Reactor
- EventMachine::Selectable
- EventMachine::SpawnedProcess
- EventMachine::StreamObject
- EventMachine::SystemCmd
- EventMachine::ThreadedResource
- EventMachine::TickLoop
- EventMachine::Timer
- EventMachine::UuidGenerator
- EventMachine::YieldBlockFromSpawnedProcess
- Object
- Object::EM
- Object::P
- BufferedTokenizer
- IO
- StringIO
- TestConnection

No matching classes.

# EventMachine::Queue

A cross thread, reactor scheduled, linear queue.

This class provides a simple queue abstraction on top of the reactor scheduler. It services two primary purposes:

- API sugar for stateful protocols

- Pushing processing onto the reactor thread

@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

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

### Public Instance Methods

<<(*items) click to toggle source
Alias for: push
empty?() click to toggle source

@return [Boolean] @note This is a peek, it's not thread safe, and may only tend toward accuracy.

    # File lib/em/queue.rb, line 54
    def empty?
      @items.empty?
    end

num_waiting() click to toggle source

@return [Integer] Waiting size @note This is a peek at the number of jobs that are currently waiting on the Queue

    # File lib/em/queue.rb, line 66
    def num_waiting
      @popq.size
    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.

@return [NilClass] nil

    # File lib/em/queue.rb, line 29
    def pop(*a, &b)
      cb = EM::Callback(*a, &b)
      EM.schedule do
        if @items.empty?
          @popq << cb
        else
          cb.call @items.shift
        end
      end
      nil # Always returns nil
    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 44
    def push(*items)
      EM.schedule do
        @items.push(*items)
        @popq.shift.call @items.shift until @items.empty? || @popq.empty?
      end
    end

Also aliased as: <<
size() click to toggle source

@return [Integer] Queue size @note This is a peek, it's not thread safe, and may only tend toward accuracy.

    # File lib/em/queue.rb, line 60
    def size
      @items.size
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.