【问题标题】:Print progress to windows cmd within clusterApply or clusterMap在 clusterApply 或 clusterMap 中将进度打印到 windows cmd
【发布时间】:2016-11-11 01:15:54
【问题描述】:

我正在使用 R 中的 snow 包在多个内核上调用 python 脚本。我想要将进度打印到控制台。在我的函数中使用cat()message()print() 不会给出任何输出。这使得跟踪我的函数的进度变得很困难。

是否可以在 clusterApply 或 clusterMap 函数中将输出打印到命令行?


这是我当前的脚本:

library(snow)
library(rlecuyer)

# Files to process
filenames=1:10

# Process function
processfunc=function(filename,filenames){
  len_names=length(filenames) #Length of filenames
  index = match(filename, filenames) #Index of current file
  cat(paste('Processing input files:',format(round(index/len_names*100,2),nsmall=2),'% At:',filename)) # print progress
  # system(paste('python','D:/pythonscript.py',filename))
}

corenr=7
cl = makeCluster(rep('localhost', corenr), 'SOCK')
clusterExport(cl, list("processfunc"))
clusterEvalQ(cl, library(stringr)) 
clusterSetupRNG(cl)
clusterMap(cl,function(x,filenames) processfunc(x,filenames),filenames,MoreArgs = list(filenames=filenames))
stopCluster(cl)

【问题讨论】:

    标签: r cmd console multiprocessing snow


    【解决方案1】:

    如果您通过终端、cmd 或 powershell 运行它,您可以添加一个额外的 systemshell 调用来打印您的字符串。例如:shell(paste('echo', 'your string'))

    工作示例

    library(snow)
    library(rlecuyer)
    
    # Files to process
    filenames=1:10
    # Process function
    processfunc=function(filename,filenames){
      len_names=length(filenames) #Length of filenames
      index = match(filename, filenames) #Index of current file
      shell(paste('echo', paste('Processing input files:',format(round(index/len_names*100,2),nsmall=2),'% At:',filename)))
      # system(paste('python','D:/pythonscript.py',filename))
    }
    
    corenr=7
    cl = makeCluster(rep('localhost', corenr), 'SOCK')
    clusterExport(cl, list("processfunc"))
    clusterEvalQ(cl, library(stringr)) 
    clusterSetupRNG(cl)
    clusterMap(cl,function(x,filenames) processfunc(x,filenames),filenames,MoreArgs = list(filenames=filenames))
    stopCluster(cl)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      • 2022-11-30
      • 2013-03-29
      相关资源
      最近更新 更多