Skip to content

Instantly share code, notes, and snippets.

@mwdchang
Created January 20, 2017 21:10
Show Gist options
  • Save mwdchang/0c62bbec437ebf2c87e76144e341dfb2 to your computer and use it in GitHub Desktop.
Save mwdchang/0c62bbec437ebf2c87e76144e341dfb2 to your computer and use it in GitHub Desktop.
Background R processing
library(parallel)
library(background)
cache = new.env();
cache[["t"]] <- "f"
#* @get /set
set <- function() {
parallelFn <- function() {
Sys.sleep(5)
r <- NULL
r["key"] <- "test"
r["val"] <- "Hello world"
r
}
p <- parallel::mcparallel( parallelFn() )
async.add(p$fd[1], function(h, p) {
async.rm(h)
print(" Inside async")
result <- parallel::mccollect(p)
print(" ")
myResult <-result[[1]]
myResult
print(" Caching...")
cache[[myResult["key"]]] <- myResult["key"]
}, p)
return("UUID of something")
}
#* @get /get
get <- function() {
print("Get")
print(cache[["t"]])
print(cache[["test"]])
return(cache[["test"]])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment