[英]R: launch separate threads for function and wait for result
I've a function fA in R calling three times the same function fB like : 我在R中有一个函数fA调用了相同函数fB的三倍,例如:
fA <- function(x){
r1 <- fB(param1)
r2 <- fB(param2)
r3 <- fB(param3)
return(c(r1,r2,r3))
}
the parameter of fBs function are computed in fA function. fBs函数的参数在fA函数中计算。 But to go faster how can I launch every fB function in background and wait for the results ( so the thre fB function are executed in parallel )
但是为了更快一点,我如何才能在后台启动每个fB函数并等待结果(因此,第三个fB函数是并行执行的)
Thanks 谢谢
Not too long ago, the parallel
package was added to the R core. 不久之前,
parallel
程序包已添加到R内核。 Have a look at functions like mclapply
and parLapply
for functions that mimic the behavior of lapply
but execute in parallel. 看一下像
mclapply
和parLapply
的函数,这些函数模拟了lapply
的行为,但是可以并行执行。 mclapply
uses process forking, and parLapply
uses clusters (eg a SOCK cluster). mclapply
使用流程派生,而parLapply
使用群集(例如SOCK群集)。 I would study the documentation of the parallel package to see what your specific situation requires. 我将研究并行程序包的文档,以了解您的具体情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.