if (FALSE) {
library(parallel)
library(future)
library(promises)
plan(multisession)
q <- queue()
# communicate from main session to child
fut <- future({
for(i in 1:1000){
Sys.sleep(.1)
q$consumer$consume()
}
})
q$producer$fireEval(stop("Stop that child"))
cat(try(value(fut)))
# Communicate from child to main session
j <- 0
fut <- future({
for(i in 1:10){
Sys.sleep(.2)
# set j in the main thread substituting i into the expression
q$producer$fireEval(j <- i, env=list(i=i))
}
})
while(j < 10){
q$consumer$consume() # collect and execute assignments
cat("j = ", j, "\n")
Sys.sleep(.1)
}
fut <- future({
for(i in 1:10){
Sys.sleep(.2)
# set j in the main thread substituting i into the expression
q$producer$fireEval(print(i), env=list(i=i))
}
})
q$consumer$start() # execute `comsume` at regular intervals
# clean up
q$destroy()
}
Run the code above in your browser using DataLab