很难调试mclapply的错误代码,因为一个作业的所有值都会受到影响。
我准备了一个简单的例子。
library(parallel)
library(dplyr)
data(iris)
## Parallel Version
parFun <- function(i){
print(i)
## Generate a random subset of the iris data set
daf <- iris[sample(1:nrow(iris),10),]
## Bug in iteration number of 39, some internal function returned NULL
if(i == 39){
daf <- NULL
}
## Dplyr produces an error, needs an if test for NULL
res <- daf %>% group_by("Species") %>% slice_min(order_by = Petal.Width, n = 2)
return(res)
}
## Do the call which returns error code
## Scheduled core 3 encountered error in user code, all values of the job will be affected
resList <- mclapply(1:50,parFun,mc.cores=12)
idx <- sapply(resList,function(x){is.null(nrow(x))})
## Depending on the number of cores a sequence of jobs is affected
which(idx == TRUE)
字符串
如何调试这样的代码进行1000次迭代?如何找到导致错误的单个i?
1条答案
按热度按时间mbzjlibv1#
在调用mclapply时,使用tryCatch块 Package parFun。代码如下:
字符串