On Tue, Jul 16, 2013 at 10:01 AM, Anthony Goins <span dir="ltr"><<a href="mailto:neontotem@gmail.com" target="_blank">neontotem@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5">On Monday, 15 July 2013 at 06:46:52 UTC, timotheecour wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Monday, 15 July 2013 at 03:49:10 UTC, Timothee Cour wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I'm trying to interact with a process using std.process and<br>
redirected stdin/stdout/stderr.<br>
What would be the recommended way?<br>
<br>
For example:<br>
----<br>
auto pipes=pipeShell("myprocess",<u></u>Redirect.all);<br>
while(true){<br>
 pipes.stdin.rawWrite(some_<u></u>command);<br>
 foreach (line; pipes.stdout.byLine) {<br>
   //do something with line<br>
 }<br>
}<br>
----<br>
<br>
This doesn't work because it might block inside pipes.stdout.byLine, as the<br>
process is requesting more inputs to be written to its stdin before<br>
outputting more bytes to its stdout.<br>
<br>
What's the right approach?<br>
* fcntl(fd, F_SETFL, O_NONBLOCK); didn't seem to work<br>
* reading pipes.stdout inside a separate thread?<br>
In that second case, how to cleanly dispose of a blocked thread when we no<br>
longer need it?<br>
<br>
Any detailed example would help.<br>
Thanks!<br>
</blockquote>
<br>
<br>
<br>
I tried using a separate thread for reading the process' stdout. It works, except that sometimes the output is shuffled out of order.<br>
<br>
Is there anything buggy in this:<br>
<br>
----<br>
__gshared string output;<br>
<br>
void readBlocking(){<br>
while ((c = fgetc(filepointer)) >= 0)<br>
      output~=cast(char) c;<br>
//NOTE: i can use something more efficient here but that's beside the question<br>
}<br>
<br>
thread = new Thread(& readBlocking);<br>
output=null;<br>
while(true){<br>
        Thread.sleep(...);<br>
        if(condition) break;<br>
}<br>
//now output is shuffled out of order sometimes<br>
----<br>
<br>
Furthermore, is there a standard way to tell when a process is waiting for stdin input ? (cf condition above). Currently I'm checking whether 'output' was modified within a timeout period T, but that's fragile and incurs of penalty of T at least.<br>

</blockquote>
<br></div></div>
Are you looking for select() or poll()<br>
Poll I believe is posix only but I think select is more widely available.<br>
Not much of an answer but I hope it helps<br>
<br>
</blockquote></div><br><div><br></div><div>Thanks, I had actually used select to solve another problem I had:</div><div>[std.process: how to process stdout chunk by chunk without waiting for process termination]</div><div>
That should work for here as well.</div><div><br></div><div>I think std.process is a bit limited currently, I keep having to implement basic stuff it doesn't support.</div>