Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
March 01, 2018 Spawning a process: Can I "have my cake and eat it too"? | ||||
---|---|---|---|---|
| ||||
I'd like to include this functionality in Scriptlike, but I don't know if it's even possible: Launch a process (spawnProcess, pipeShell, etc) so the child's stdout/stderr go to the parent's stdout/stderr *without* the possibility of them getting inadvertently reordered/reinterleaved when viewed on the terminal, *and* still allow the parent to read the child's stdout and stderr? How could this be accomplished? Is it even possible? |
March 02, 2018 Re: Spawning a process: Can I "have my cake and eat it too"? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky (Abscissa) | I would suggest redirecting the child to the parent pipe, but then having the parent write the data back out to its own stdout/err. It'd be a bit tricky with just Phobos' file though because it doesn't make it easy to wait for or be notified about input on it, but the underlying OS apis make this reasonably simple (WaitForMultipleObjects or select) if you're willing to write a bit more code to do it yourself or at least pull the underlying handles out of the Phobos file. |
March 02, 2018 Re: Spawning a process: Can I "have my cake and eat it too"? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky (Abscissa) | On Friday, 2 March 2018 at 04:50:06 UTC, Nick Sabalausky (Abscissa) wrote:
> Launch a process (spawnProcess, pipeShell, etc) so the child's stdout/stderr go to the parent's stdout/stderr *without* the possibility of them getting inadvertently reordered/reinterleaved when viewed on the terminal, *and* still allow the parent to read the child's stdout and stderr?
I think it's possible on Linux.
1. Disable buffering on the pipe (see stdbuf etc.)
2. Fake output type to fool isatty (e.g. script -c 'child_program args...' /dev/null)
|
March 02, 2018 Re: Spawning a process: Can I "have my cake and eat it too"? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky (Abscissa) | On 3/1/18 11:50 PM, Nick Sabalausky (Abscissa) wrote:
> I'd like to include this functionality in Scriptlike, but I don't know if it's even possible:
>
> Launch a process (spawnProcess, pipeShell, etc) so the child's stdout/stderr go to the parent's stdout/stderr *without* the possibility of them getting inadvertently reordered/reinterleaved when viewed on the terminal, *and* still allow the parent to read the child's stdout and stderr?
>
> How could this be accomplished? Is it even possible?
You'd have to do this in the parent. You can duplicate the file descriptor, so that writing to either goes to the same spot, but you can't "fork" the file descriptor, so that writing to one goes to 2 spots.
With Phobos's std.process, you would have to allocate a buffer to store the data as you read from the pipe and wrote it to the terminal, it wouldn't be automatic.
I can think of an easy way to do it in iopipe, but it still would be dependent on the reader actually reading the data from the pipe. In other words, it wouldn't come out when the child wrote, it would only come out when the parent read. But this might be inherent in what you want, because the only way to prevent interleaving is to control it all from one place.
-Steve
|
March 02, 2018 Re: Spawning a process: Can I "have my cake and eat it too"? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On 3/2/18 9:23 AM, Steven Schveighoffer wrote:
> On 3/1/18 11:50 PM, Nick Sabalausky (Abscissa) wrote:
>> How could this be accomplished? Is it even possible?
>
> You'd have to do this in the parent. You can duplicate the file descriptor, so that writing to either goes to the same spot, but you can't "fork" the file descriptor, so that writing to one goes to 2 spots.
Hm.. you could potentially do this via an intermediary process that does the copying. Then you could avoid writing the buffering code in your parent or child.
i.e. something like the Unix tee command.
-Steve
|
Copyright © 1999-2021 by the D Language Foundation