[racket] fun with ports and (system ..) calls

From: Vlad Kozin (vladilen.kozin at gmail.com)
Date: Wed Oct 16 13:48:57 EDT 2013

Yep, closing the port did the trick. Thanks David and Matthew. 

I wonder though if this is practical in a general case. Say, I expect more data and want to grab it as it appears. Something like calling "tail -10" on a file that's being updated. I thought flushing the port would do, but it doesn't.

Here's an example of IO from the Guide:
> (define-values (in out) (make-pipe))
> (write "hello" out)
> (read in)
> (write '("alphabet" soup) out)
> (read in)
'("alphabet" soup)

Why does this work?

Vlad Kozin <vladilen.kozin at gmail.com>

On Oct 16, 2013, at 1:36 PM, David Vanderson wrote:

> On 10/16/2013 12:35 PM, Vlad Kozin wrote:
>> assuming I have a number of .c files I expect "find" to write corresponding lines to (current-output-port). I want to collect them in a list. Here's a solution I thought would work:
>> ------------------------------------
>> (define-values (in out) (make-pipe))
>> (parameterize ((current-output-port out))
>>   (system (format "find . ~a ~a" "-name \"*.c\"" "-print"))
>>   (for/list ((line (in-lines in)))
>>     line))
>> It doesn't. It blocks expecting input.
>> Blocks again. Am I not getting EOF here?
> I think you are on the right track here.  You need to close the output port on the pipe so "in-lines" stops reading:
> (define-values (in out) (make-pipe))
> (parameterize ((current-output-port out))
>  (system (format "find . ~a ~a" "-name \"*.c\"" "-print"))
>  (close-output-port (current-output-port))
>  (for/list ((line (in-lines in)))
>    line))
> Does that make sense?
> Alternatively you can rearrange your last example:
> (define str (with-output-to-string
>               (lambda ()
>                 (system (format "find . ~a ~a" "-name \"*.c\"" "-print")))))
> (for/list ((line (in-lines (open-input-string str))))
>  line)
> Thanks,
> Dave

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20131016/a06c207f/attachment-0001.html>

Posted on the users mailing list.