r - How to log when using foreach (print or futile.logger) -


i want use foreach package in conjunction logging. use futile.logger package. when work given workers logging information lost (which strange need indicate foreach logging package)

i've seen this post not use foreach

  library(foreach)                                                                                                                                                                                                                                                                                                          library(futile.logger)                                                                                                                                                                                                                                                                                                    library(doparallel)                                                                                                                                                                                                                                                                                                       flog.threshold(debug)                                                                                                                                                                                                                                                                                                     cluster <- makecluster(8)   registerdoparallel(cluster)   dostuff <- function(input){                                                                                                                                                                                                                                                                                                 flog.debug('doing stuff %s', input)                                                                                                                                                                                                                                                                           return(input)                                                                                                                                                                                                                                                                                                         }                                                                                                                                                                                                                                                                                                                         res <- lapply(fun=dostuff, x=seq(1,8,1))   # >> prints                                                                                                                                                                                                                                                                            res2 <- foreach(input = seq(1,8,1)) %do% dostuff(input)                                                                                                                                                                                                                                                                   # >> prints   res3 <- foreach(input = seq(1,8,1), .packages='futile.logger') %dopar% dostuff(input)           # >> not                                                                                                                                                                                                                             identical(res,res2) && identical(res,res3) 

i not care parallele backend, can anything, how can symply logging working

following solution how can print when using %dopar%: idea use snow set cluster, , set outfile="" redirect worker output master.

library(foreach) library(futile.logger) library(doparallel)  library(dosnow) cluster <- makecluster(3, outfile="") # have 4 cores, 8 registerdosnow(cluster) flog.threshold(debug)  dostuff <- function(input){   flog.info('doing stuff %s', input) # change flog.info   return(input)    }  res <- lapply(fun=dostuff, x=seq(1,8,1)) # >> prints                                                               res2 <- foreach(input = seq(1,8,1)) %do% dostuff(input)  # >> prints res3 <- foreach(input = seq(1,8,1), .packages='futile.logger') %dopar% dostuff(input)   # >> prints 

output:

> res3 <- foreach(input = seq(1,8,1), .packages='futile.logger') %dopar% dostuff(input)   type: exec  type: exec  type: exec  type: exec  type: exec  type: exec  info [2016-08-08 08:22:39] doing stuff 3 type: exec  info [2016-08-08 08:22:39] doing stuff 1 info [2016-08-08 08:22:39] doing stuff 2 type: exec  type: exec  info [2016-08-08 08:22:39] doing stuff 5 info [2016-08-08 08:22:39] doing stuff 4 type: exec  type: exec  info [2016-08-08 08:22:39] doing stuff 6 info [2016-08-08 08:22:39] doing stuff 7 info [2016-08-08 08:22:39] doing stuff 8 

output log file. here's alternative outputs log file, following how log using futile logger within parallel method in r?. has advantage of having cleaner output, still requires flog.info:

library(dosnow) library(foreach) library(futile.logger) nworkers <- 3 cluster <- makecluster(nworkers) registerdosnow(cluster) loginit <- function(logfile) flog.appender(appender.file(logfile)) foreach(input=rep('~/desktop/out.log', nworkers),    .packages='futile.logger') %dopar% loginit(input) dostuff <- function(input){   flog.info('doing stuff %s', input)   return(input)    }  foreach(input = seq(1,8,1), .packages='futile.logger') %dopar% dostuff(input)  stopcluster(cluster) readlines("~/desktop/out.log") 

output:

> readlines("~/desktop/out.log") [1] "info [2016-08-08 10:07:30] doing stuff 2" [2] "info [2016-08-08 10:07:30] doing stuff 1" [3] "info [2016-08-08 10:07:30] doing stuff 3" [4] "info [2016-08-08 10:07:30] doing stuff 4" [5] "info [2016-08-08 10:07:30] doing stuff 5" [6] "info [2016-08-08 10:07:30] doing stuff 6" [7] "info [2016-08-08 10:07:30] doing stuff 7" [8] "info [2016-08-08 10:07:30] doing stuff 8" 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -