-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
merging stdout/stderr cleanly #43
Comments
@dch you mean just getting stdout & stderr data in a single stream? Something like this? script = "for s in $(seq 1 10); do echo stdout $s; echo stderr $s >&2; done"
cmd = ["sh", "-c", script]
Exile.stream!(cmd, stderr: :consume, ignore_epipe: true)
|> Stream.map(fn {_io_stream, data} -> data end)
|> Enum.to_list()
|> IO.puts() Producing output
Or like preserving the natural write ordering as in the second example? Preserving the order is interesting, I initially skipped because merging stdio streams can cause lot of problems when binary data is involved. But I guess it makes sense to support it via explicit flag for those who know what they are doing. |
I will try to come up with a (short) explanation of the risks involved here, but yes if it is possible to use the same file descriptor/handle inside the NIF for this case, then at least text-based tools that alternate output between stderr/stdout can work as expected, without needing to wrap the shell. For threaded, forked processes, or raw binary data this will obviously produce garbage, but this is also likely to be relatively obvious during even basic tests. |
Yeah, that makes sense. |
this works very nicely, thanks. How is this for a sample explanation? Using
|
Hey @dch, that's much better than I could have come up with! |
Note the difference between these 2 shell scripts:
Evidently this is all related to buffering inside the kernel, depending on how fast
the NIF handles data before the next read, and there may not be a lot to work
around this.
Is it possible to add a further stderr mode that handles the redirection already?
stderr: :merge
or similar, such that its not necessary to wrap a shell invocation around?Or are there other options to handle this?
The text was updated successfully, but these errors were encountered: