我正在使用 parLapply
并行运行一个函数,如下所示:
parLapply(cl, df, fun = SomeFunction)
其中cl
是我事先注册的集群。
但是函数执行时间太长,我想在执行时间太长时退出。
我知道R.utils
包有一个withTimeOut
函数,它工作得很好(当有循环退出时 - apply本质上是一个循环)。
并行应用系列是否有类似的功能?
最佳答案
使用与 parLapply 非常相似的 foreach,我可以这样做:
library(doParallel)
registerDoParallel(cl <- makeCluster(2))
foreach(ic = 1:5) %dopar% {
withCallingHandlers({
setTimeLimit(elapsed = 4, transient = TRUE)
Sys.sleep(ic)
}, error = function(e) stop("timeout"))
}
stopCluster(cl)
关于r - parLapply 超时选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53502219/