module EventMachine

Public Class Methods

synchrony(blk=nil, tail=nil) { || ... } click to toggle source

A convenience method for wrapping a given block within a Ruby Fiber such that async operations can be transparently paused and resumed based on IO scheduling. It detects whether EM is running or not.

# File lib/em-synchrony.rb, line 26
def self.synchrony(blk=nil, tail=nil)
  # EM already running.
  if reactor_running?
    if block_given?
      Fiber.new { yield }.resume
    else
      Fiber.new { blk.call }.resume
    end
    tail && add_shutdown_hook(tail)

  # EM not running.
  else
    if block_given?
      run(nil, tail) { Fiber.new { yield }.resume }
    else
      run(Proc.new { Fiber.new { blk.call }.resume }, tail)
    end

  end
end