[racket] turning input-port into file-stream port
On Tue, Nov 12, 2013 at 11:55:22PM -0500, David T. Pierson wrote:
> On Nov 12, 2013, at 4:04 PM, Vlad Kozin wrote:
> > Can somebody pls have a look at this code. I can't figure out why it
> > doesn't work. Input is certainly seen inside the background thread
> > that I spawn to pump data from one port to another. But the main
> > thread seems to receive an empty port.
>
> Hi Vlad,
>
> You have a synchronization problem. The copy-port within foo starts
> reading the input file stream and hits EOF before the copy-port within
> the separate thread starts writing to the file. Adding a sleep (hack
> alert!) before returning from make-file-stream-input-port makes the
> output appear:
>
> http://pasterack.org/pastes/630
My sleep hack was obviously not a real solution, so I came up with the
following, assuming you really do want to force a file stream port:
#lang racket
(define (make-file-stream-input-port in-port)
(if (file-stream-port? in-port)
in-port
(match (process*/ports #f in-port 'stdout "/bin/cat")
((list stdout-in-port #f pid #f proc)
stdout-in-port))))
(define input-string (open-input-string "hello\nworld\n"))
(define input-file-stream (make-file-stream-input-port input-string))
(copy-port input-file-stream
(current-output-port))
(close-input-port input-file-stream)
Hope that helps.
David