[racket] turning input-port into file-stream port
> 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.
David Pierson, you're absolutely right. This is exactly the problem with my hacky solution, which your (sleep 1) addition sort of solves.
> Vlad, are you looking for pipes?
Matthias, I'm trying re-create unix shell pipe behavior, but where you can splice Racket code wrapped in (begin ..) as part of the pipeline: every element in the pipe reads from (current-input-port). (subprocess ..) that I was using to run external binaries strictly expects file-stream-ports. In Racket however my 'pipe' form can be parametrized over any kind of input/output port: string, result of
(make-pipe), etc. Hence the need to somehow wrap the input ports.
More insight came courtesy of #racket irc particularly Jens Axel Søgaard. As was pointed out to me process*/ports could be just what I was looking for and I should be using it in place of (subprocess ..). Cursory glance at the source code for process*/ports reveals that it calls the subprocess and does exactly the port manipulation magic I was looking for. I don't pretend to understand the code since I'm yet to learn concurrent programming in Racket, but this answer by Matthew Flatt seems to support my understanding.
> #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))))
David Pierson, calling external "cat" to fake what essentially is copying one port onto another won't cut it (pun intended) :) I hope the guts of process*/ports will provide the solution. But the behavior of your code is exactly what I need.
---
Vlad Kozin <vladilen.kozin at gmail.com>
On Nov 13, 2013, at 8:58 AM, Matthias Felleisen wrote:
>
> On Nov 12, 2013, at 11:55 PM, David T. Pierson <dtp at mindstory.com> wrote:
>
>> Do you really need a file stream port and not just an input port?
>
> Vlad, are you looking for pipes?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20131113/1228adf6/attachment.html>