我正在尝试使用partitions
和pblapply
处理大型分区数据帧parallel
。但是,每个线程似乎都无法访问全局变量。
a
是另一个大型数据框架。如何使a
可以访问该功能?
请参阅下面的虚拟脚本:
library(pbapply)
library(parallel)
partitions <- list(c(1:5),
c(6:10),
c(11:15))
a <- 1
myfn <- function(x, add = a) {
out = x + a
return(out)
}
pblapply(nums, myfn,
cl = makeCluster(detectCores() - 1))
这会引发错误:
Error in checkForRemoteErrors(val) :
3 nodes produced errors; first error: object 'a' not found
你应该明确传递add
参数:
cl <- makeCluster(detectCores() - 1)
pblapply(
partitions,
function(x, add) x + add,
cl = cl,
add = a
)
stopCluster(cl)