[plt-scheme] subprocess and wait

From: Paulo J. Matos (pocmatos at gmail.com)
Date: Mon Feb 27 09:54:29 EST 2006

On 26/02/06, Eli Barzilay <eli at barzilay.org> wrote:
> On Feb 26, Paulo J. Matos wrote:
> > On 26/02/06, Eli Barzilay <eli at barzilay.org> wrote:
> > >
> > > A side-comment: running a subprocess that generates lots of
> > > outputs and collecting all that text in the scheme process might
> > > not be a good idea since you're making that other process compete
> > > with scheme for memory.  My guess is that switching things so you
> > > process the subprocess output in a thread is going to be just a
> > > little bit more complex.
> >
> > I guess you're right, but what would then be your suggestion? Doing
> > the parsing of the output of the process during its execution?
> 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.

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?


Paulo Matos

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

Paulo Jorge Matos - pocm at sat inesc-id pt
Web: http://sat.inesc-id.pt/~pocm
Computer and Software Engineering

Posted on the users mailing list.