Created
February 14, 2015 18:08
-
-
Save zdennis/50abf06ddc23eb4506b1 to your computer and use it in GitHub Desktop.
Ruby Process.spawn with pipes
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
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