Class/Module Index [+]

Quicksearch

EventMachine::Synchrony

Public Class Methods

add_periodic_timer(interval, &blk) click to toggle source

Fiber-aware EventMachine timer: wraps the passed in block within a new fiber (new fiber on every invocation) to allow you to continue using synchrony methods

# File lib/em-synchrony.rb, line 105
def self.add_periodic_timer(interval, &blk)
  EM.add_periodic_timer(interval) do
    Fiber.new { blk.call }.resume
  end
end
add_timer(interval, &blk) click to toggle source

Fiber-aware EventMachine timer: wraps the passed in block within a new fiber context such that you can continue using synchrony methods

# File lib/em-synchrony.rb, line 95
def self.add_timer(interval, &blk)
  EM::Timer.new(interval) do
    Fiber.new { blk.call }.resume
  end
end
defer(op = nil, &blk) click to toggle source

Fiber-aware EM.defer

# File lib/em-synchrony.rb, line 119
def self.defer op = nil, &blk
  fiber = Fiber.current
  EM.defer(op || blk, lambda{ |result| fiber.resume(result) })
  Fiber.yield
end
gets() click to toggle source

Routes to EM::Synchrony::Keyboard

# File lib/em-synchrony.rb, line 135
def self.gets
  EventMachine::Synchrony::Keyboard.new.gets
end
next_tick(&blk) click to toggle source

Fiber-aware EM.next_tick convenience function

# File lib/em-synchrony.rb, line 113
def self.next_tick(&blk)
  EM.next_tick { Fiber.new { blk.call }.resume }
end
sleep(secs) click to toggle source

Fiber-aware sleep function using an EM timer

Execution is stopped for specified amount of seconds and then automatically resumed (just like regular sleep) except without locking the reactor thread

# File lib/em-synchrony.rb, line 85
def self.sleep(secs)
  fiber = Fiber.current
  EM::Timer.new(secs) { fiber.resume }
  Fiber.yield
end
sync(df) click to toggle source

sync is a close relative to inlineCallbacks from Twisted (Python)

Synchrony.sync allows you to write sequential code while using asynchronous or callback-based methods under the hood. Example:

result = EM::Synchrony.sync EventMachine::HttpRequest.new(URL).get p result.response

As long as the asynchronous function returns a Deferrable object, which has a "callback" and an "errback", the sync methond will automatically yield and automatically resume your code (via Fibers) when the call either succeeds or fails. You do not need to patch or modify the Deferrable object, simply pass it to EM::Synchrony.sync

# File lib/em-synchrony.rb, line 62
def self.sync(df)
  f = Fiber.current
  xback = proc do |*args|
    if f == Fiber.current
      return args.size == 1 ? args.first : args
    else
      f.resume(*args)
    end
  end

  df.callback(&xback)
  df.errback(&xback)

  Fiber.yield
end
system(cmd, *args) click to toggle source

Fiber-aware EM.system

# File lib/em-synchrony.rb, line 127
def self.system cmd, *args
  fiber = Fiber.current
  EM.system(cmd, *args){ |out, status| fiber.resume( [out, status] ) }
  Fiber.yield
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.