r - Modify variables outside function using mclapply -
it easy modify variables outside function using assign()
or <<-
, if function called lapply()
. these tricks seem not working while calling function mclapply()
, parallel version of lapply()
in package multicore
:
require(multicore) f <- function(i) { x[i] <- x[i] + 1 y[i] <<- y[i] + 1 } x <- y <- 1:10 invisible(lapply(1:5, f)) x # 1 2 3 4 5 6 7 8 9 10, not changed y # 2 3 4 5 6 6 7 8 9 10, variables 1 5 changed # running mclapply x <- y <- 1:10 invisible(mclapply(1:5, f)) x # 1 2 3 4 5 6 7 8 9 10, not changed y # 1 2 3 4 5 6 7 8 9 10, not changed again!
how same task - modifying variables outside function - while calling function mclapply()
?
i know should used care, because parallel running functions may try modify same variable can cause severe synchronization errors, helpful in cases.
Comments
Post a Comment