1

我不明白foreach使用doMC后端时正在导出什么。

有没有办法控制运送给工人的东西?

以下示例似乎表明全局环境中的所有内容都已交付,这在我使用 100+ Gb 表时给我带来了麻烦。

library("foreach")                                                                                                             
library("doMC")                                                                                                                
registerDoMC(3L)                                                                                                               
x <- 1                                                                                                                         
y <- 2                                                                                                                         
foo <- function(x) x+1                                                                                                         
cat(sprintf("hostname: %s, pid: %s, Objects are: %s\n", Sys.getenv("HOSTNAME"), Sys.getpid(), paste(ls(), collapse = ",")))    

foreach(task = 1:3, .noexport = ls()) %dopar%                                                                                  
{                                                                                                                              
    cat(sprintf("hostname: %s, pid: %s, Objects are: %s\n", Sys.getenv("HOSTNAME"), Sys.getpid(), paste(ls(), collapse = ",")))
    foo(task)                                                                                                                  
} -> res                                                                                                                       
print(res)   

结果: 工人如何知道 foo ?

hostname: nyzls604m, pid: 372957, Objects are: foo,res,x,y
hostname: nyzls604m, pid: 385492, Objects are: task       
hostname: nyzls604m, pid: 385493, Objects are: task       
hostname: nyzls604m, pid: 385494, Objects are: task       
[[1]]                                       
[1] 2                                       

[[2]]                                       
[1] 3                                       

[[3]]                                       
[1] 4                                       
4

0 回答 0