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

From: David Vanderson (david.vanderson at gmail.com)
Date: Wed Oct 16 14:36:36 EDT 2013

Also if you are primarily interested in getting the output of "system" 
calls, you can use "process" or "subprocess" instead to get results back 
asynchronously.  Remember to close ports as per the documentation.

On 10/16/2013 02:30 PM, Matthew Flatt wrote:
> At Wed, 16 Oct 2013 13:48:57 -0400, Vlad Kozin wrote:
>> 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.
> When you use `(for/list ((line (in-lines in))) ...)` the `in-lines`
> sequence doesn't end until an EOF is available --- but a line can be
> received anyway, as long as newline is found to terminate the line.
>
> For example,
>
>   (define-values (in out) (make-pipe))
>   (parameterize ((current-output-port out))
>      (system (format "find . ~a ~a" "-name \"*.c\"" "-print")))
>   (for ((line (in-lines in)))
>     (displayln line))
>
> doesn't end, since `out` is never closed, but lines are parsed and
> displayed along the way.
>
> Similarly, in
>
>> Here's an example of IO from the Guide:
>> ---------------------------------------
>> Examples:
>>> (define-values (in out) (make-pipe))
>>> (write "hello" out)
>>> (read in)
>> "hello"
>>> (write '("alphabet" soup) out)
>>> (read in)
>> '("alphabet" soup)
>>
>> Why does this work?
> the closing quotes allow "hello" to be read, and the closing
> parenthesis allows '("alphabet" soup) to be read, even though no
> end-of-file is available for the input end of the pipe.
>
>


Posted on the users mailing list.