[plt-scheme] writing to subprocess's stdin and then...

From: YC (yinso.chen at gmail.com)
Date: Tue Jun 17 03:41:14 EDT 2008

Thanks Eli - please see inline.

On Mon, Jun 16, 2008 at 10:40 PM, Eli Barzilay <eli at barzilay.org> wrote:

>
> You have the rough idea, but several problems:
>
> * you have a redundant `lambda' which means that you never really get
>  to read anything (on toy examples, this will probably be a change
>  that will make things work, but see below)
>

Yup - it worked after removing the lambda.


> * `write' is probably not a good idea -- it will print something with
>  the double quotes.
>

Makes sense.


> * after you write, you'd want to flush `pin', or if you expect the
>  program to get just that, then you'd want to close it (so your
>  program will not sit there waiting for more stuff)
>

Ah ha!  I first tried to write eof to the stream and it didn't work...
thanks for the pointer!


>
> * finally, the biggest problem is a conceptual one: you read stuff
>  from the output only after the process has finished -- but what if
>  it spits out a lot of data?  In that case, it will not finish, and
>  instead just sit there waiting for you to read that data, and you'll
>  be getting into a very common race condision with subprocesses.
>
>  What you really need is to do the reading in a thread, so the
>  process can continue running.  It might seem strange at first, but
>  when there's a lot of data then *someone* needs to hold it, and the
>  OS will hold only a very small amount (and for good reasons).  Your
>  thread will need to do just that accumulation (or it can just to the
>  processing, whatever it is).
>

After re-reading your example, I think I started to grok what you were doing
on http://www.cs.brown.edu/pipermail/plt-scheme/2006-February/011953.html:

...
(define-values (in out) (make-pipe)) ...
...
(thread (lambda ()
          (copy-port pout out)
          (close-output-port out)
          (subprocess-wait p)))

You first created a pipe for holding the accumulation, and then you started
a thread to read the data from pout into pipe's out, and when out is closed
the data gets piped to in (perhaps this is happening in the background
without you have to close it too?), and finally the process exits...
correct?  But shouldn't the ports be closed after subprocess-wait?


>
> (You can also see "collects/mzlib/process.ss" for a very extensive
> solution.)
>

Thanks for the pointer - time to learn the concurrency primitives in the
module... ;)

Thanks,
yc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20080617/42571076/attachment.html>

Posted on the users mailing list.