Created
August 29, 2011 13:54
-
-
Save marianposaceanu/1178438 to your computer and use it in GitHub Desktop.
io_pipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
# r, w = IO.pipe | |
# w << 'secret' | |
# w << ' message' | |
# w.close_write | |
# r.read #=> "secret message" | |
# if fork | |
# wr.close | |
# puts "Parent got: <#{rd.read}>" | |
# rd.close | |
# Process.wait | |
# else | |
# rd.close | |
# puts "Sending message to parent" | |
# wr << "Hi Dad" << " Bye Mom" | |
# wr.close | |
# end | |
# the pipe | |
rd, wr = IO.pipe | |
# creates a new process | |
# this should happen in the worker | |
process_pid = fork do | |
Signal.trap("HUP") do | |
puts "Ouch!"; exit | |
end | |
for i in (1..12) | |
sleep 1 | |
wr << "{no_of_messages:#{i}}\n" unless i == 5 | |
if i == 5 | |
wr << "{end:true}\n" | |
end | |
end | |
end | |
# gets messages | |
# this should be in the manager | |
until rd.closed? do | |
buf = rd.readpartial(4096) | |
puts buf | |
if buf == "{end:true}\n" then | |
rd.close_read | |
Process.kill("HUP", process_pid) | |
end | |
end | |
# Process.wait | |
# wr.close_write | |
# puts rd.read | |
# puts rd.flush.inspect | |
# aux = rd.readpartial(4096) | |
# puts 'before' | |
# rd.each_line do |l| | |
# puts l | |
# puts rd.inspect | |
# | |
# Process.kill("HUP", process_pid) if l == "{end:true}\n" | |
# break if rd.closed? | |
# end | |
# puts 'after' | |
# res = aux.split(',') | |
# puts res.inspect | |
# | |
# res.each |r| | |
# puts JSON.parse(r).inspect | |
# end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment