Skip to content

Instantly share code, notes, and snippets.

@ccutrer
Last active December 14, 2017 04:42
Show Gist options
  • Save ccutrer/d3b8ee33e73a51c48894c5dfd26dc67e to your computer and use it in GitHub Desktop.
Save ccutrer/d3b8ee33e73a51c48894c5dfd26dc67e to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'timeout'
trap(:CHLD) { }
x = 0
while true
puts "loop #{x += 1}"
pid = Process.spawn('sleep 1')
Timeout.timeout(30) do
Process.waitpid(pid)
end
end
#!/usr/bin/env ruby
require 'timeout'
self_pipe = IO.pipe
signal_queue = []
def wake_up(self_pipe)
self_pipe[1].write_nonblock('.', exception: false)
end
trap(:CHLD) { signal_queue << :CHLD; wake_up(self_pipe) }
signal_processor = Thread.new do
loop do
self_pipe[0].read(1)
signal_queue.pop
end
end
x = 0
while true
puts "loop #{x += 1}"
pid = Process.spawn('sleep 1')
Timeout.timeout(30) do
Process.waitpid(pid)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment