[plt-scheme] subprocess and wait

From: Eli Barzilay (eli at barzilay.org)
Date: Mon Feb 27 10:26:37 EST 2006

On Feb 27, Paulo J. Matos wrote:
> On 26/02/06, Eli Barzilay <eli at barzilay.org> wrote:
> > Yes.  Something like this:
> >
> >   (require (lib "port.ss"))
> >   (define (with-input-from-subprocess exe thunk)
> >     (define-values (in out) (make-pipe))
> >     (define-values (p pout pin perr)
> >       (subprocess #f (open-input-file "/dev/null") (current-error-port) exe))
> >     (thread (lambda ()
> >               (copy-port pout out)
> >               (close-output-port out)
> >               (subprocess-wait p)))
> >     (parameterize ([current-input-port in]) (thunk)))
> >
> >   (require (lib "string.ss"))
> >   (printf ">> ~s\n"
> >           (with-input-from-subprocess "/bin/pwd"
> >             (lambda () (regexp-split #rx"/" (read-line)))))
> >
> Yes, didn't know about copy-port, thanks. Still, it's not working as
> I expected, since thunk is reading data and returning when
> eof-object?  is returned by (read-line). But this happens when no
> data is there, but the process might still be running. So probably I
> only stop parsing the output when the process stops and not when
> eof-object? is found, right? Or is there a better way.

Why?  Once you get an eof, there is nothing else that you can parse.
If you want to avoid starting the next job while this one is still
running, you can turn the body into (begin0 (thunk) (subprocess-wait p)).

> On a more technical side of the code. Is there any special reason
> why you used define-values and not let-values? Wouldn't it be
> considered 'more correct' (whatever that means) to use let-values?

No, no reason.

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.