如何在并行包中跟踪R中mclapply的进度

for*_*ter 4 parallel-processing r lapply mclapply

我的问题与这个问题有关.但是,上面引用的问题使用的multicore是被替换的包parallel.无法在parallel包中复制响应中的大多数功能.有没有办法跟踪进度mclapply.在查看mclapply文档时,有一个名为的参数mc.silent,我不确定这是否能够跟踪进度,如果可以,我们如何以及在哪里可以看到日志文件?我在ubuntulinux OS 上运行.请参阅下面的重复性示例,我想对其进行处理.

require(parallel) 

wait.then.square <- function(xx){
  # Wait for one second
  Sys.sleep(2);
  # Square the argument 
  xx^2 } 

output <- mclapply( 1:10, wait.then.square, mc.cores=4,mc.silent=FALSE)
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.

thi*_*e1e 5

借助该软件包,pbmcapply您现在可以轻松跟踪进度mclapplymcmapply作业.只需替换mclapplypbmclapply:

wait.then.square <- function(xx) {
    Sys.sleep(2)
    xx^2 
} 

library(pbmcapply)
output <- pbmclapply(1:10, wait.then.square, mc.cores = 4)
Run Code Online (Sandbox Code Playgroud)

...这将显示一个漂亮的进度条.

笔者对技术细节和性能基准一篇好的博客贴子在这里.


fot*_*ton 4

这是我相关答案的更新。

library(parallel)

finalResult <- local({
  f <- fifo(tempfile(), open="w+b", blocking=T)
  if (inherits(parallel:::mcfork(), "masterProcess")) {
    # Child
    progress <- 0.0
    while (progress < 1 && !isIncomplete(f)) {
      msg <- readBin(f, "double")
      progress <- progress + as.numeric(msg)
      cat(sprintf("Progress: %.2f%%\n", progress * 100))
    } 
    parallel:::mcexit()
  }
  numJobs <- 100
  result <- mclapply(1:numJobs, function(...) {
    # Do something fancy here... For this example, just sleep
    Sys.sleep(0.05)
    # Send progress update
    writeBin(1/numJobs, f)
    # Some arbitrary result
    sample(1000, 1)
  })
  close(f)
  result
})

cat("Done\n")
Run Code Online (Sandbox Code Playgroud)