Is there way to track progress on a mclapply?

前端 未结 6 1129
忘了有多久
忘了有多久 2020-12-23 11:11

I love the setting .progress = \'text\' in plyr\'s llply. However, it causes my much anxiety to not know how far along an mclapp

6条回答
  •  臣服心动
    2020-12-23 11:29

    Based on the answer of @fotNelson, using a progress bar instead of line by line printing and calling an external function with mclapply.

    library('utils')
    library('multicore')
    
    prog.indic <- local({ #evaluates in local environment only
        f <- fifo(tempfile(), open="w+b", blocking=T) # open fifo connection
        assign(x='f',value=f,envir=.GlobalEnv)
        pb <- txtProgressBar(min=1, max=MC,style=3)
    
        if (inherits(fork(), "masterProcess")) { #progress tracker
            # Child
            progress <- 0.0
            while (progress < MC && !isIncomplete(f)){ 
                msg <- readBin(f, "double")
                    progress <- progress + as.numeric(msg)
    
                # Updating the progress bar.
                setTxtProgressBar(pb,progress)
                } 
    
    
            exit()
            }
       MC <- 100
       result <- mclapply(1:MC, .mcfunc)
    
        cat('\n')
        assign(x='result',value=result,envir=.GlobalEnv)
        close(f)
        })
    
    .mcfunc<-function(i,...){
            writeBin(1, f)
            return(i)
        }
    

    Assigning the fifo connection to the .GlobalEnv is necessary to use it from a function outside of the mclapply call. Thanks for the questions and the previous replies, I had been wondering how to do this for a while.

提交回复
热议问题