Closed dch closed 5 months ago
Hi @dch,
Are you are thinking of an interface similar to the one provided by Port
?
Currently there is no way to read data without caller explicitly calling read
functions, to make it demand-driven.
You can simulate port like behavior by spawning a separate process, but I am not sure if that answers your question.
parent = self()
spawn_link(fn ->
Exile.stream(~w"ping -c 3 localhost", stderr: :consume)
|> Enum.each(&Process.send(parent, &1, [:noconnect]))
end)
You can also use Exile.Process
directly for finer control over reading.
defmodule Test do
def run do
parent = self()
spawn_link(fn ->
{:ok, proc} = Exile.Process.start_link(~w"ping -c 3 localhost", stderr: :consume)
loop(parent, proc)
end)
end
def loop(parent, proc) do
case Exile.Process.read_any(proc) do
{:ok, data} ->
send(parent, data)
loop(parent, proc)
:eof ->
:ok
error ->
raise error
end
end
end
thanks for the examples. This is better than what I've come up with so far thanks.
Firstly this is a lovely crafted & documented library. thanks!
I want to receive each response from Exile directly into a calling GenServer, as messages. As a simple example,
ping(8)
anddbg/1
are used.Is there a simpler way to do this, perhaps using
Exile.Process
directly somehow?