Hello,
I'm experimenting about using external commands, which act as filters (i.e. read from standard input and write to standard output):
(define p (open-process (list path: "/bin/head")))
(define t (make-thread (lambda () (let loop () (define line (read-line p)) (if (eof-object? line) line (begin (display "[") (display line) (display "]\n") (thread-yield!) (loop)))))))
(thread-start! t)
(display "a\nb\nc\n" p) (display "d\ne\nf\ng\nh\ni\nj\nk\nl\n" p) (force-output p) (thread-join! t)
Here's the corresponding output:
[a] [b] [c] [d] [e] [f] [g] [h] [i] [j]
No problem so far. This case is easy, because the external command /bin/head terminates automatically after having filtered the first 10 lines.
But there are filters, which will never terminate automatically, e.g. /bin/cat. In that case I want the main thread stopping filtering.
How can that be done? By adding (close-port p) somewhere? How should the example look like, if /bin/head is replaced by /bin/cat, and the sending thread decides to terminate after having sent 10 lines. The output should be the same as from the original example, of course.
Regards Thomas
Afficher les réponses par date
Thomas Hafner hafner@sdf-eu.org wrote/schrieb 20050602120503.06BBC6040F86@faun.hafner.NL.EU.ORG:
How can that be done? By adding (close-port p) somewhere? How should the example look like, if /bin/head is replaced by /bin/cat, and the sending thread decides to terminate after having sent 10 lines. The output should be the same as from the original example, of course.
No longer help needed, because I've found a solution. After having written the last part of data, the sending thread just calls close-output-port rather than close-port:
(define p (open-process (list path: "/bin/cat")))
(define t (make-thread (lambda () (display "a\nb\nc\n" p) (display "d\ne\nf\ng\nh\ni\nj\n" p) (close-output-port p))))
(thread-start! t)
(let loop () (let ((line (read-line p))) (if (eof-object? line) line (begin (display "[") (display line) (display "]\n") (loop)))))
Regards Thomas