I want to use startProcess where I both read and write to the process. The only code I have that works is hopefully included below. The problem is that the process hangs unless I close the input handle. The doc says not to do this because the process close will do it. I tried fdatasync instead of the close on the input handle but that does not seem to work. I would appreciate any suggestions for a better way to do it.
import osproc
from posix import read, write, fdatasync, close
var p = startProcess("/usr/bin/cat","",["-"])
var fd1 = outputHandle(p)
var fd0 = inputHandle(p);
var s0 = "This is a test\n"
discard write(fd0, s0.cstring, s0.len)
discard fd0.close
var buf=newstring(10)
var s=""
while true:
var rc=read(fd1, buf.cstring, buf.len)
if rc<=0: break
s.add(buf[0..rc-1])
close(p)
echo s
This is because cat will not write its input until it receives an EOF or its buffer is full. The reason why fdatasync() doesn't work is that it can only ensure that data is written to the pipe, it cannot make cat actually process it.
Note also that if you do both input and output with an external process, you may need threads/select/poll in order to avoid deadlock.