if(interactive()){
# ------ Basic example
library(future)
library(dipsaus)
# sequential
plan("sequential")
make_forked_clusters()
plan() # multicore, or multisession (on windows)
Sys.getpid() # current main session PID
value(future({Sys.getpid()})) # sub-process PID, evaluated as multicore
# ------ When fork is not supported
# reset to default single core strategy
plan("sequential")
# Disable forked process
options("dipsaus.no.fork" = TRUE)
options("dipsaus.cluster.backup" = "multisession")
# Not fall back to multisession
make_forked_clusters()
plan()
# ------ Auto-clean
# reset plan
plan("sequential")
options("dipsaus.no.fork" = FALSE)
options("dipsaus.cluster.backup" = "multisession")
# simple case:
my_func <- function(){
make_forked_clusters(clean = TRUE)
fs <- lapply(1:4, function(i){
future({Sys.getpid()})
})
unlist(value(fs))
}
my_func() # The PIDs are different, meaning they ran in other sessions
plan() # The plan is sequential, auto reversed strategy
# ------ Auto-clean with lapply_async2
my_plan <- plan()
# lapply_async2 version of the previous task
lapply_async2(1:4, function(i){
Sys.getpid()
})
identical(plan(), my_plan)
}
Run the code above in your browser using DataLab