Sha256: e6aca1668f014d8b62891a90a8120f0897d339888319276a8df32e04a4346d0e

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

EMFORK = $0 == __FILE__

if EMFORK
  require 'rubygems'
end

require 'eventmachine'

#:stopdoc:

# helper to fork off EM reactors
def EM.fork num = 1, &blk
  unless @forks
    trap('CHLD'){
      pid = Process.wait
      p [:pid, pid, :died] if EMFORK
      block = @forks.delete(pid)
      EM.fork(1, &block)
    }

    trap('EXIT'){
      p [:pid, Process.pid, :exit] if EMFORK
      @forks.keys.each{ |pid|
        p [:pid, Process.pid, :killing, pid] if EMFORK
        Process.kill('USR1', pid)
      }
    }
    
    @forks = {}
  end

  num.times do
    pid = EM.fork_reactor do
      p [:pid, Process.pid, :started] if EMFORK

      trap('USR1'){ EM.stop_event_loop }
      trap('CHLD'){}
      trap('EXIT'){}

      blk.call
    end

    @forks[pid] = blk
    p [:children, EM.forks] if EMFORK
  end
end

def EM.forks
  @forks.keys
end

if EMFORK
  p 'starting reactor'

  trap('INT'){ EM.stop_event_loop }

  EM.run{
    p [:parent, Process.pid]

    EM.fork(2){
      EM.add_periodic_timer(1) do
        p [:fork, Process.pid, :ping]
      end
    }

  }

  p 'reactor stopped'
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
tmm1-amqp-0.6.0 lib/ext/emfork.rb
tmm1-amqp-0.6.1 lib/ext/emfork.rb
amqp-0.6.0 lib/ext/emfork.rb