使用%dopar%时如何打印 [英] How can I print when using %dopar%

查看:72
本文介绍了使用%dopar%时如何打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个foreach循环,该循环使用%dopar%doSNOW作为后端.如何让循环在每次迭代中打印出一些内容?

I have a foreach loop that uses %dopar% with doSNOW as the back-end. How can I have the loop print something out each iteration?

我下面的代码是我当前正在使用的代码,但未打印任何内容.

My code below is what I'm currently using, but its not printing anything.

foreach(ntree=rep(25,2),.combine=combine,.packages='randomForest',
    .inorder=FALSE) %dopar% {
        print("RANDOM FOREST")
        randomForest(classForm,data=data,na.action=na.action,do.trace=do.trace,ntree=ntree,mtry=mtry)
    }   

推荐答案

此处发布了许多好的解决方案,但我发现最简单的方法是登录到套接字并使用单独的过程在控制台中输出日志调用

There are a number of good solutions posted here, but I find it easiest to log to a socket and use a separate process to output the log calls in a console.

我使用以下功能:

log.socket <- make.socket(port=4000)

Log <- function(text, ...) {
  msg <- sprintf(paste0(as.character(Sys.time()), ": ", text, "\n"), ...)
  cat(msg)
  write.socket(log.socket, msg)
}

然后您可以将日志语句放置在代码中,例如:

You can then place log statements in the code such as:

Log("Processing block %d of %d", i, n.blocks)

可以使用任何简单的套接字侦听工具实时查看日志输出.例如,在Linux上使用netcat:

Log output can viewed in real-time using any simple socket listening tool. For example, using netcat on Linux:

nc -l 4000

以上日志语句将在netcat终端中显示为:

The above log statement would display in the netcat terminal as:

2014-06-25 12:30:45: Processing block 2 of 13

此方法的优点是可以远程工作,并提供您希望记录的详细输出.

This method has the advantage of working remotely and provides as detailed output as you care to log.

ps 对于Windows上的操作系统,请参见乔恩·克雷顿(Jon Craton)的netcat端口

p.s. For those on Windows, see Jon Craton's netcat port.

p.p.s 我猜想write.socket R函数可能不是线程安全的,但是除非您频繁登录,否则您不太可能遇到任何问题.不过要注意一点.

p.p.s I'm guessing the write.socket R function probably isn't thread-safe, but unless you're logging at high frequency, you're unlikely to run into any issue. Something to be aware of though.

这篇关于使用%dopar%时如何打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆