Skip to content

Instantly share code, notes, and snippets.

@stevedomin
Last active August 29, 2015 14:16
Show Gist options
  • Save stevedomin/c0bfa8d0a2be9c5c2380 to your computer and use it in GitHub Desktop.
Save stevedomin/c0bfa8d0a2be9c5c2380 to your computer and use it in GitHub Desktop.
Porcelain spawn/receive
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()
@eksperimental
Copy link

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment