使用并行函数时 R CRAN 检查失败

R CRAN Check fail when using parallel functions

我想使用 parallel::makeCluster(parallel::detectCores()).

向使用并行计算的 CRAN 提交一个包

当我构建包时一切正常,但是当我检查包时 (devtools::check(document = FALSE)) returns 错误:

Running examples in ‘TESTER-Ex.R’ failed
The error most likely occurred in:

> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
> ### Name: hello_world
> ### Title: Prints hello world
> ### Aliases: hello_world
> 
> ### ** Examples
> 
> hello_world()
Error in .check_ncores(length(names)) : 8 simultaneous processes spawned
Calls: hello_world -> <Anonymous> -> makePSOCKcluster -> .check_ncores
Execution halted

我在 MWE 包 (TESTER) 中重新创建了错误,它只有一个功能 hello_world,我已将整个 TESTER-package 上传到 GitHub 但它应该是可从以下函数重现。

#' Prints hello world
#'
#' @return nothing
#' @export
#'
#' @examples
#' hello_world()
hello_world <- function() {

  # initiate cluster
  cl <- parallel::makeCluster(parallel::detectCores())

  # stop cluster
  parallel::stopCluster(cl)

  cat("Hello World\n")
  return(invisible(NULL))
}

我已经查看 Writing R Extensions 但找不到与此问题相关的任何内容,我也找不到关于 SO 的问题。

知道导致此错误的原因以及解决方法吗?

CRAN 将包可用的核心数限制为 2, 出于性能原因。 邮件列表中有一个线程, 我相信, 但我现在找不到它。

我在测试中使用了类似下面的东西:

chk <- Sys.getenv("_R_CHECK_LIMIT_CORES_", "")

if (nzchar(chk) && chk == "TRUE") {
    # use 2 cores in CRAN/Travis/AppVeyor
    num_workers <- 2L
} else {
    # use all cores in devtools::test()
    num_workers <- parallel::detectCores()
}

我认为问题是没有加载并行库

在您的 NAMESPACE 文件中,您应该加载包

import(parallel)