Skip to content

Instantly share code, notes, and snippets.

@jstorimer
Created April 18, 2012 19:29
Show Gist options
  • Save jstorimer/2415944 to your computer and use it in GitHub Desktop.
Save jstorimer/2415944 to your computer and use it in GitHub Desktop.
require 'socket'
require 'kgio'
socket = Kgio::TCPServer.new '127.0.0.1', 4481
concurrency = 3
child_pids = []
sig_queue = []
concurrency.times do
child_pids << fork do
trap(:INT) { exit }
# heartbeat timestamp
loop do
connection = socket.kgio_tryaccept
if connection
connection.write connection.read
connection.close
end
end
end
end
trap(:CHLD) do
sig_queue << :CHLD
end
trap(:INT) do
sig_queue << :INT
end
loop do
case sig_queue.shift
when :INT
child_pids.each do |cpid|
Process.kill(:INT, cpid)
sleep 0.5
end
exit
when :CHLD
puts 'child terminated unexpectedly'
end
end
#Process.waitall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment