-
-
Save Bogdanp/ff6e609f9a6ff964efdd12ff5948a252 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket/base | |
(require racket/format) | |
(define (read-bytes/lazy n in [bufsize 4096]) | |
(define buf (make-bytes bufsize)) | |
(define offset | |
(let loop ([offset 0]) | |
(define len | |
(peek-bytes-avail! buf offset #f in)) | |
(cond | |
[(eof-object? len) offset] | |
[else | |
(define offset* (+ offset len)) | |
(cond | |
[(> offset* n) offset*] | |
[else (loop offset*)])]))) | |
(cond | |
[(zero? offset) eof] | |
[else (read-bytes (min offset n) in)])) | |
(define-syntax-rule (mem e ...) | |
(begin | |
(collect-garbage) | |
(define s (current-memory-use)) | |
(begin0 (begin e ...) | |
(printf "mem delta: ~a\n" (~r (/ (- (current-memory-use) s) | |
1024 | |
1024)))))) | |
(define N (* 100 1024 1024)) | |
(let () | |
(define-values (in out) | |
(make-pipe)) | |
(define t1 | |
(thread | |
(lambda _ | |
(mem | |
(displayln "before") | |
(printf "~v\n" (read-bytes/lazy N in)) | |
(displayln "after"))))) | |
(sleep 1) | |
(displayln "hello" out) | |
(displayln " how are you" out) | |
(close-output-port out) | |
(sync t1)) | |
(let () | |
(define-values (in out) | |
(make-pipe)) | |
(define t2 | |
(thread | |
(lambda _ | |
(mem | |
(displayln "before") | |
(printf "~v\n" (read-bytes N in)) | |
(displayln "after"))))) | |
(sleep 1) | |
(displayln "hello" out) | |
(displayln " how are you" out) | |
(close-output-port out) | |
(sync t2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment