Skip to content

Instantly share code, notes, and snippets.

@lian
Created November 6, 2013 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lian/7346060 to your computer and use it in GitHub Desktop.
Save lian/7346060 to your computer and use it in GitHub Desktop.
em-fork-reactor.rb
require 'eventmachine'
children = []
Signal.trap('INT') do
puts "Killing #{children.size} children ..."
children.each do |pid|
Process.kill('SIGUSR1', pid) rescue nil
end
EM.stop
end
EM.epoll if EM.epoll?
EM.run do
channel = EM::Channel.new
puts "Father PID: #{Process.pid}"
channel.subscribe {|m| puts "\t#{m}" }
EM.add_periodic_timer(1) do
puts "Father(#{Process.pid}): #{Time.now.to_s}"
end
4.times do |i|
pid = EM.fork_reactor do
Signal.trap('SIGUSR1') { EM.stop rescue nil }
EM.add_periodic_timer(1) do
channel.push "Child[#{i}](#{Process.pid}): #{Time.now.to_s}"
end
end
children << pid
puts "Child[#{i}] PID: #{pid}"
Process.detach(pid)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment