Interleave reading/writing to subprocess in process-pipe-bytevector,

using u8-ready? to avoid blocking.  Fixes issue #253.
This commit is contained in:
Alex Shinn 2015-04-15 11:24:56 +09:00
parent aa3f869798
commit 4267164e92

View file

@ -17,12 +17,29 @@
(call-with-process-io (call-with-process-io
cmd cmd
(lambda (pid proc-in proc-out proc-err) (lambda (pid proc-in proc-out proc-err)
;; This could overflow the pipe. (let ((len (bytevector-length bvec))
(write-bytevector bvec proc-in) (out (open-output-bytevector)))
(close-output-port proc-in) (let lp ((i 0))
(let ((res (port->bytevector proc-out))) (cond
(waitpid pid 0) ((u8-ready? proc-out)
res)))) (let ((u8 (read-u8 proc-out)))
(cond
((eof-object? u8)
(get-output-bytevector out))
(else
(write-u8 u8 out)
(lp i)))))
((< i len)
(write-u8 (bytevector-u8-ref bvec i) proc-in)
(if (= len (+ i 1))
(close-output-port proc-in))
(lp (+ i 1)))
(else
;; Once we've completed sending the input we busy wait
;; until all output has been read. We can't just waitpid
;; here because the remaining output may still overflow the
;; pipe buffer.
(lp i))))))))
;;> Gzip compress a string or bytevector in memory. ;;> Gzip compress a string or bytevector in memory.