使用 foreach 时如何记录(打印或 futile.logger)
How to log when using foreach (print or futile.logger)
我想将 foreach
包与日志记录结合使用。我通常使用 futile.logger
包。当工作交给工人时,日志信息丢失(这很奇怪,因为您需要指示 foreach 日志包)
我见过 this post 但它没有使用 foreach
library(foreach)
library(futile.logger)
library(doParallel)
flog.threshold(DEBUG)
cluster <- makeCluster(8)
registerDoParallel(cluster)
doStuff <- function(input){
flog.debug('Doing some stuff with %s', input)
return(input)
}
res <- lapply(FUN=doStuff, X=seq(1,8,1))
# >> this prints
res2 <- foreach(input = seq(1,8,1)) %do% doStuff(input)
# >> this prints
res3 <- foreach(input = seq(1,8,1), .packages='futile.logger') %dopar% doStuff(input)
# >> this does not
identical(res,res2) && identical(res,res3)
我不太关心并行后端,可以是任何东西,但我怎样才能让日志正常工作
遵循 How can I print when using %dopar% 的解决方案:想法是使用 snow
设置集群,并设置 outfile=""
将 worker 输出重定向到 master。
library(foreach)
library(futile.logger)
library(doParallel)
library(doSNOW)
cluster <- makeCluster(3, outfile="") # I only have 4 cores, but you could do 8
registerDoSNOW(cluster)
flog.threshold(DEBUG)
doStuff <- function(input){
flog.info('Doing some stuff with %s', input) # change to flog.info
return(input)
}
res <- lapply(FUN=doStuff, X=seq(1,8,1))
# >> this prints
res2 <- foreach(input = seq(1,8,1)) %do% doStuff(input)
# >> this prints
res3 <- foreach(input = seq(1,8,1), .packages='futile.logger') %dopar% doStuff(input)
# >> this prints too
输出:
> 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 some stuff with 3
Type: EXEC
INFO [2016-08-08 08:22:39] Doing some stuff with 1
INFO [2016-08-08 08:22:39] Doing some stuff with 2
Type: EXEC
Type: EXEC
INFO [2016-08-08 08:22:39] Doing some stuff with 5
INFO [2016-08-08 08:22:39] Doing some stuff with 4
Type: EXEC
Type: EXEC
INFO [2016-08-08 08:22:39] Doing some stuff with 6
INFO [2016-08-08 08:22:39] Doing some stuff with 7
INFO [2016-08-08 08:22:39] Doing some stuff with 8
输出到日志文件。这是在 How to log using futile logger from within a parallel method in R? 之后输出到日志文件的替代方法。它的优点是输出更清晰,但仍然需要 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 some stuff with %s', input)
return(input)
}
foreach(input = seq(1,8,1), .packages='futile.logger') %dopar% doStuff(input)
stopCluster(cluster)
readLines("~/Desktop/out.log")
输出:
> readLines("~/Desktop/out.log")
[1] "INFO [2016-08-08 10:07:30] Doing some stuff with 2"
[2] "INFO [2016-08-08 10:07:30] Doing some stuff with 1"
[3] "INFO [2016-08-08 10:07:30] Doing some stuff with 3"
[4] "INFO [2016-08-08 10:07:30] Doing some stuff with 4"
[5] "INFO [2016-08-08 10:07:30] Doing some stuff with 5"
[6] "INFO [2016-08-08 10:07:30] Doing some stuff with 6"
[7] "INFO [2016-08-08 10:07:30] Doing some stuff with 7"
[8] "INFO [2016-08-08 10:07:30] Doing some stuff with 8"
我想将 foreach
包与日志记录结合使用。我通常使用 futile.logger
包。当工作交给工人时,日志信息丢失(这很奇怪,因为您需要指示 foreach 日志包)
我见过 this post 但它没有使用 foreach
library(foreach)
library(futile.logger)
library(doParallel)
flog.threshold(DEBUG)
cluster <- makeCluster(8)
registerDoParallel(cluster)
doStuff <- function(input){
flog.debug('Doing some stuff with %s', input)
return(input)
}
res <- lapply(FUN=doStuff, X=seq(1,8,1))
# >> this prints
res2 <- foreach(input = seq(1,8,1)) %do% doStuff(input)
# >> this prints
res3 <- foreach(input = seq(1,8,1), .packages='futile.logger') %dopar% doStuff(input)
# >> this does not
identical(res,res2) && identical(res,res3)
我不太关心并行后端,可以是任何东西,但我怎样才能让日志正常工作
遵循 How can I print when using %dopar% 的解决方案:想法是使用 snow
设置集群,并设置 outfile=""
将 worker 输出重定向到 master。
library(foreach)
library(futile.logger)
library(doParallel)
library(doSNOW)
cluster <- makeCluster(3, outfile="") # I only have 4 cores, but you could do 8
registerDoSNOW(cluster)
flog.threshold(DEBUG)
doStuff <- function(input){
flog.info('Doing some stuff with %s', input) # change to flog.info
return(input)
}
res <- lapply(FUN=doStuff, X=seq(1,8,1))
# >> this prints
res2 <- foreach(input = seq(1,8,1)) %do% doStuff(input)
# >> this prints
res3 <- foreach(input = seq(1,8,1), .packages='futile.logger') %dopar% doStuff(input)
# >> this prints too
输出:
> 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 some stuff with 3
Type: EXEC
INFO [2016-08-08 08:22:39] Doing some stuff with 1
INFO [2016-08-08 08:22:39] Doing some stuff with 2
Type: EXEC
Type: EXEC
INFO [2016-08-08 08:22:39] Doing some stuff with 5
INFO [2016-08-08 08:22:39] Doing some stuff with 4
Type: EXEC
Type: EXEC
INFO [2016-08-08 08:22:39] Doing some stuff with 6
INFO [2016-08-08 08:22:39] Doing some stuff with 7
INFO [2016-08-08 08:22:39] Doing some stuff with 8
输出到日志文件。这是在 How to log using futile logger from within a parallel method in R? 之后输出到日志文件的替代方法。它的优点是输出更清晰,但仍然需要 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 some stuff with %s', input)
return(input)
}
foreach(input = seq(1,8,1), .packages='futile.logger') %dopar% doStuff(input)
stopCluster(cluster)
readLines("~/Desktop/out.log")
输出:
> readLines("~/Desktop/out.log")
[1] "INFO [2016-08-08 10:07:30] Doing some stuff with 2"
[2] "INFO [2016-08-08 10:07:30] Doing some stuff with 1"
[3] "INFO [2016-08-08 10:07:30] Doing some stuff with 3"
[4] "INFO [2016-08-08 10:07:30] Doing some stuff with 4"
[5] "INFO [2016-08-08 10:07:30] Doing some stuff with 5"
[6] "INFO [2016-08-08 10:07:30] Doing some stuff with 6"
[7] "INFO [2016-08-08 10:07:30] Doing some stuff with 7"
[8] "INFO [2016-08-08 10:07:30] Doing some stuff with 8"