Is there way to track progress on a mclapply?

前端 未结 6 1130
忘了有多久
忘了有多久 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:50

    Due to the fact that mclapply spawns multiple processes, one might want to use fifos, pipes, or even sockets. Now consider the following example:

    library(multicore)
    
    finalResult <- local({
        f <- fifo(tempfile(), open="w+b", blocking=T)
        if (inherits(fork(), "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))
            } 
            exit()
        }
        numJobs <- 100
        result <- mclapply(1:numJobs, function(...) {
            # Dome something fancy here
            # ...
            # Send some progress update
            writeBin(1/numJobs, f)
            # Some arbitrary result
            sample(1000, 1)
        })
        close(f)
        result
    })
    
    cat("Done\n")
    

    Here, a temporary file is used as fifo, and the main process forks a child whose only duty is to report the current progress. The main process continues by calling mclapply where the expression (more precisely, the expression block) that is to be evaluated writes partial progress information to the fifo buffer by means of writeBin.

    As this is only a simple example, you'll probably have to adapt the whole output stuff to your needs. HTH!

提交回复
热议问题