Skip to content

Instantly share code, notes, and snippets.

@randy3k
Created December 5, 2021 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randy3k/56111a1bec60e26a8905637154ba459e to your computer and use it in GitHub Desktop.
Save randy3k/56111a1bec60e26a8905637154ba459e to your computer and use it in GitHub Desktop.
R fifo example
tf <- tempfile()
f <- fifo(tf, "w+b", blocking = TRUE)
p <- callr::r_bg(function(tf) {
f <- fifo(tf, "wb", blocking = TRUE)
x <- rnorm(1e6)
b <- serialize(x, NULL)
writeBin(length(b), f)
writeBin(b, f)
close(f)
},
list(tf = tf))
n <- readBin(f, "integer", n = 1L)
cat("n =", n, "\n")
r <- raw(0)
while (length(r) < n) {
r <- append(r, readBin(f, "raw", n = n - length(r)))
}
length(unserialize(r))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment