Last active
August 29, 2015 14:16
-
-
Save stevedomin/c0bfa8d0a2be9c5c2380 to your computer and use it in GitHub Desktop.
Porcelain spawn/receive
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
alias Porcelain.Process | |
alias Porcelain.Result | |
defmodule Cmd do | |
def run() do | |
args = ["b"] | |
opts = [ | |
in: "a\nb\nc\nb\nb\nd\nb\ne", | |
out: {:send, self()}, | |
err: {:send, self()} # eksperimental -> This is the only thing that changed | |
] | |
%Process{pid: pid} = Porcelain.spawn("grep", args, opts) | |
handle_output(pid) | |
end | |
def handle_output(pid) do | |
receive do | |
{^pid, :data, :out, data} -> | |
IO.puts "data: #{data}" | |
handle_output(pid) | |
{^pid, :data, :err, data} -> | |
IO.puts "error: #{data}" | |
handle_output(pid) | |
{^pid, :result, %Result{status: status}} -> | |
IO.puts "status: #{status}" | |
after | |
5_000 -> IO.puts "timeout" | |
end | |
end | |
end | |
Cmd.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@stevedomin, i just noticed, data is being received one one chunk by handle_output,
I would like to include a country as line by line is received. any idea how to do this?