baris.aydek
2019-7-25 10:05:39

Hi :wave:. How do I get the PDF or kindle version of HTDP book?


synth
2019-7-25 11:05:51

The Kindle edition can be obtained from Amazon at least :slightly_smiling_face:


mark.warren
2019-7-25 12:14:44

Me again. Does anyone know if you are allowed to have an input and output port open on a file at the same time?



greg
2019-7-25 13:02:28

(That returns two values (input and output port). So typically you’d use with define-values to bind to two variables.)


mark.warren
2019-7-25 13:02:56

@greg Argh, missed that, thanks, I’ve really got to learn to RTFM a bit more closely.


greg
2019-7-25 13:04:53

Well, it’s not in the Guide IIRC. Would need to look in the Reference under file ports. And although it’s certainly not a “weird” thing to do (read/write to the same file), it’s not something I’ve happened to do a lot, not happened to see in other code lately. So I kind of “knew” it was possible but had to check. :simple_smile:


soegaard2
2019-7-25 13:06:49

I am wondering whether using open-input-file and open-output-file separately is better (unless it is a special port such as COM1)?


mark.warren
2019-7-25 13:07:13

It does imply that it’s not a usual thing to do. It also implies that you can open separate input and output ports on the same file, maybe I should just give it a go and see.


greg
2019-7-25 13:07:16

Oh derp I didn’t read the docs carefully.


greg
2019-7-25 13:07:32

I think @soegaard2 is right.


greg
2019-7-25 13:07:48

Sorry.


mark.warren
2019-7-25 13:08:09

Not a problem, all knowledge is good.


greg
2019-7-25 13:08:39

That’s probably the actual reason I’ve hardly ever seen open-input-output-file in code. :simple_smile:


soegaard2
2019-7-25 13:09:39

Wonder how it works. If I write to the output port, will the input port know that it needs to refresh its buffers?


soegaard2
2019-7-25 13:09:52

Probably not.


mark.warren
2019-7-25 13:10:27

As I say, probably best for me to try, and see what happens.


mark.warren
2019-7-25 13:11:00

What’s the worst that can happen right :grin:


greg
2019-7-25 13:11:53

grabs popcorn


soegaard2
2019-7-25 13:12:19

pauses Tour de France


mark.warren
2019-7-25 13:12:55

Ok, I’m not that fast. Give me a mo.


mark.warren
2019-7-25 13:23:30

@greg @soegaard2 Ok, initially the input port does not see the changes that the output port has made, so I probably need to do a refresh somehow, or reopen the input port, hmm.


soegaard2
2019-7-25 13:25:28

FWIW it is also possible to disable buffering, but I don’t know how it affects throughput.



mark.warren
2019-7-25 13:30:25

@soegaard2 Yep, that seems to work, as you say though, how it affects throughput will have to be seen.


mark.warren
2019-7-25 13:31:06

Really quick test code #lang racket (define out (open-output-file "../io.txt" #:exists 'can-update)) (define in (open-input-file "../io.txt")) (file-stream-buffer-mode out 'none) (write-string "test" out) (displayln (read-line in)) (close-input-port in) (close-output-port out)


greg
2019-7-25 13:59:04

If totally disabling buffering turns out to be too slow, you can also leave it on and flush-output-port as necessary. (Of course now “as necessary” becomes a potential source of bugs.)


greg
2019-7-25 14:01:22

For certain kinds of ports like TCP you can’t disable buffering, so you need to flush manually. e.g. At the end of a request, before waiting to read back some response. That’s not such a tricky thing to get correct in most programs. It’s just a mistake everyone seems to make, at first. :simple_smile:


greg
2019-7-25 14:01:48

“why does my client get st…” “flush-output-port”


greg
2019-7-25 14:01:51

:smile:


mark.warren
2019-7-25 14:01:52

Hmmm… Something I need to try later


mark.warren
2019-7-25 14:02:32

More experimentation required