[plt-scheme] subprocess and wait

From: Eli Barzilay (eli at barzilay.org)
Date: Sun Feb 26 15:07:17 EST 2006

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)))))


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


Posted on the users mailing list.