Skip to content

Instantly share code, notes, and snippets.

@zdennis
Created February 14, 2015 18:08
Show Gist options
  • Save zdennis/50abf06ddc23eb4506b1 to your computer and use it in GitHub Desktop.
Save zdennis/50abf06ddc23eb4506b1 to your computer and use it in GitHub Desktop.
Ruby Process.spawn with pipes
100.times do
r,w = IO.pipe
pid1 = Process.spawn(Hash.new, "echo", "foo\nbar\nbaz\nfood", out: w)
pid2 = Process.spawn(Hash.new, "grep", "foo", in: r)
foo = Process.waitpid2(pid1)
# don't forget to close the write pipe, otherwise this will hang on the second process indefinitely
w.close
bar = Process.waitpid2(pid2)
# this isn't necessary since the write end of the pipe has been closed,
r.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment