问题
Racket has the nice read-bytes-async!
function, which I believe exists in every other programming language in the world. It reads what it can from an input stream, without blocking, into a buffer, returning the number of bytes written.
Said function seems like an absolutely essential function for efficiently implementing, say, the Unix cat
tool, yet Chicken Scheme seems to lack any such function. Of course, I can use (read-byte)
and (write-byte)
, but that is slow and eats up all my CPU.
Even (copy-port)
seems to not have any such implementation. Instead, before the stream is closed, the data is copied buffer-by-buffer only when the buffers fill. This means that (copy-port (current-input-port) (current-output-port))
does not behave like cat
at all.
Am I just suffering from a terrible blind spot in reading the documentation, or does Chicken shockingly actually lack such a function? So cat
can't even be written efficiently in Chicken?
回答1:
I fixed my problem. The posix
library has the file-read
function that does what I want, albeit on a file descriptor. Fortunately, ports in Chicken are just thin wrappers around file descriptors; there is a port to file descriptor converter in the posix
library as well.
Interestingly, these functions work on Windows as well. posix
seems to not be limited to POSIX systems.
回答2:
as you said the posix unit is the key ,but to your question what seems more relevant is set-buffering-mode!
this applies to any port.
来源:https://stackoverflow.com/questions/19718767/buffered-i-o-in-chicken-scheme