首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >parLapply -如何解决错误“找不到函数”bindToEnv“?

parLapply -如何解决错误“找不到函数”bindToEnv“?
EN

Stack Overflow用户
提问于 2020-01-15 10:37:23
回答 1查看 1.4K关注 0票数 4

我想使用parLapply,并且按照这里介绍的方式设置代码:http://www.win-vector.com/blog/2016/01/parallel-computing-in-r/

在过去的几次中,它工作得很好。然而,在我当前的parLapply调用中,我得到了错误Error in checkForRemoteErrors(val) : 3 nodes produced errors; first error: could not find function "bindToEnv"

下面是一个简短的示例:

代码语言:javascript
运行
复制
#' Copy arguments into env and re-bind any function's lexical scope to bindTargetEnv .
#' 
#' See http://winvector.github.io/Parallel/PExample.html for example use.
#' 
#' 
#' Used to send data along with a function in situations such as parallel execution 
#' (when the global environment would not be available).  Typically called within 
#' a function that constructs the worker function to pass to the parallel processes
#' (so we have a nice lexical closure to work with).
#' 
#' @param bindTargetEnv environment to bind to
#' @param objNames additional names to lookup in parent environment and bind
#' @param names of functions to NOT rebind the lexical environments of
bindToEnv <- function(bindTargetEnv=parent.frame(),objNames,doNotRebind=c()) {
  # Bind the values into environment
  # and switch any functions to this environment!
  for(var in objNames) {
    val <- get(var,envir=parent.frame())
    if(is.function(val) && (!(var %in% doNotRebind))) {
      # replace function's lexical environment with our target (DANGEROUS)
      environment(val) <- bindTargetEnv
    }
    # assign object to target environment, only after any possible alteration
    assign(var,val,envir=bindTargetEnv)
  }
}

ccc <- 1

# Parallel
cl <- parallel::makeCluster(getOption("cl.cores", 3))
junk <- parallel::clusterEvalQ(cl, c(library(data.table)))

f <- function(x) {
  bindToEnv(objNames = 'ccc')

  return(x+x)  
}

b <- do.call(rbind, parallel::parLapply(cl, 1:10,  f))

如果我不添加bindToEnv,一切都会正常工作。我做错了什么?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-16 13:57:21

在创建集群之前,您需要使用clusterExport()导出您定义的已用函数和对象。

代码语言:javascript
运行
复制
library(parallel)
cl <- makeCluster(getOption("cl.cores", 3))
clusterEvalQ(cl, c(library(data.table)))
clusterExport(cl, c("bindToEnv", "ccc"), 
              envir=environment())
f <- function(x) {
  bindToEnv(objNames='ccc')
  return(x+x)  
}

b <- do.call(rbind, parallel::parLapply(cl, 1:10,  f))
b
#        ,1]
#  [1,]    2
#  [2,]    4
#  [3,]    6
#  [4,]    8
#  [5,]   10
#  [6,]   12
#  [7,]   14
#  [8,]   16
#  [9,]   18
# [10,]   20

stopCluster(cl)
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59744462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档