Skip to content

Instantly share code, notes, and snippets.

@dselivanov
Last active October 2, 2017 05:41
Show Gist options
  • Save dselivanov/cd0d41d9eaf9807ccda4627ee7463483 to your computer and use it in GitHub Desktop.
Save dselivanov/cd0d41d9eaf9807ccda4627ee7463483 to your computer and use it in GitHub Desktop.
server <- function(){
while(TRUE) {
writeLines("Listening...")
con <- socketConnection(host="localhost", port = 6011, blocking=TRUE,
server=TRUE, open="w+b")
writeLines("con done")
x = unserialize(con)
writeLines("readRDS done")
writeLines(capture.output(str(x)))
serialize(list(response = Sys.time(), x = x), con)
close(con)
}
}
server()
send_worker <- function(x) {
con <- socketConnection(host="localhost", port = 6011, blocking=TRUE, server=FALSE, open="w+b")
on.exit(close(con))
write_resp <- saveRDS(x, con)
server_resp <- unserialize(con)
server_resp
}
send_worker(1)
library(background)
future = function(expr, e) {
p = parallel::mcparallel(expr)
async.add(p$fd[1], function(h, kv) {
expr = kv[[1]]
v = kv[[2]]
async.rm(h)
v[["res"]] = parallel::mccollect(expr)[[1]]
# print(v[["res"]])
}, list(p, e))
TRUE
}
e = new.env(parent = emptyenv())
temp = future({ Sys.sleep(2); "done!" }, e)
e[["res"]]
#NULL
Sys.sleep(2.1);
e[["res"]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment