Pieter Breed
Pieter Breed

Reputation: 5689

Why does this call to (read in-from-make-pipe) block in racket?

I have this code from the documentation:

(define-values (in out) (make-pipe))
(write "234234" out)
(read in)

This produces "234234" like in the docs. This next piece of code, just blocks on the read. Why does this happen?

(define-values (in out) (make-pipe))
(write 234234 out) ; <-- NOT A STRING
(read in) ;          <-- BLOCKS

Upvotes: 4

Views: 73

Answers (1)

John Clements
John Clements

Reputation: 17233

The underlying problem here is that the reader must parse a complete value from the input. When you send "234234" to the pipe, the pipe contains 8 characters, and the last one (the second double-quote) informs read that the value is complete. When you write 234234, the only thing in the pipe are the digits, and the reader can't tell whether the number is complete. To see this, try the following:

#lang racket

(define-values (in out) (make-pipe))
(write 234234 out)
(write 111 out)
(display " " out)
(read in)

this produces the number 234234111.

Upvotes: 4

Related Questions