Module: EventMachine

Defined in:
lib/em-synchrony.rb,
lib/em-synchrony/amqp.rb,
lib/em-synchrony/thread.rb,
lib/em-synchrony/em-http.rb,
lib/em-synchrony/em-multi.rb,
lib/em-synchrony/em-redis.rb,
lib/em-synchrony/iterator.rb,
lib/em-synchrony/keyboard.rb,
lib/em-synchrony/tcpsocket.rb,
lib/em-synchrony/em-hiredis.rb,
lib/em-synchrony/fiber_iterator.rb,
lib/em-synchrony/connection_pool.rb

Defined Under Namespace

Modules: HTTPMethods, Hiredis, Protocols, Synchrony

Class Method Summary collapse

Class Method Details

.synchrony(blk = nil, tail = nil) ⇒ Object

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.


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/em-synchrony.rb', line 25

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