我正在尝试使用 mclapply 创建要在 R 中并行运行的任务列表。
library(parallel)
tasks <- list(
job1 = y(5),
job2 = y(6)
)
# Using fork()
out <- mclapply(
tasks,
function(f) f(),
mc.cores = length(tasks)
)
其中 y 是例如:
y<-function(x){
a<-x^2
return(a)
}
创建列表时,它使用参数执行函数,而不是仅将其存储为列表。(所以这是在实际到达之前mclapply
。)如果我使用y
没有任何参数的函数,这种方法运行良好:
tasks <- list(
job1 = y,
job2 = y
)
# Using fork()
out <- mclapply(
tasks,
function(f) f(),
mc.cores = length(tasks)
)
其中 y 是例如:
y<-function(){
a<-5^2
return(a)
}
那么如何在不执行函数的情况下将带参数的函数存储在列表中(直到我在 mclapply 中告诉它们)?