与`withCallingHandlers`并行出错

时间:2018-11-15 07:27:11

标签: r foreach try-catch snow

我有一个类似的并行过程:

library(foreach)
library(doSNOW)

cl = makeCluster(parallel::detectCores() - 1, type = "SOCK", outfile = "out.txt") 
registerDoSNOW(cl)

l = foreach(i = 1:100) %dopar% { 
  res = withCallingHandlers(
    read.csv("somefilethatdoesntexist.csv"), error = function(e) e)
  if(inherits(res, "error")) res = NULL
  res
}

我的期望是,即使“表达式”中存在错误,循环也应继续,但是它会退出并出现错误,并且不会创建结果“ l”变量。

这似乎与丢失的文件特别相关。但是,如果我将其包装在tryCatch中并在“表达式”中进行适当处理,怎么会出错呢?

1 个答案:

答案 0 :(得分:3)

也许是这样(改编自here):

library(foreach)
library(doSNOW)

cl = makeCluster(parallel::detectCores() - 1, type = "SOCK", outfile = "out.txt") 
registerDoSNOW(cl)

l = foreach(i = 1:2) %dopar% { 
  withCallingHandlers({
      res <- withRestarts( read.csv("somefilethatdoesntexist.csv"),
                          skipError=function() return(NULL))
    },
    error=function(e) {saveRDS(e, paste0("E:/temp/", i, ".rds")); invokeRestart("skipError")})
  res
}

l
#[[1]]
#NULL
#
#[[2]]
#NULL

e <- readRDS("E:/temp/1.rds")
e
#<simpleError in file(file, "rt"): cannot open the connection>