5

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

是否可以在 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)
4

1 回答 1

0

如果您通过终端、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)
于 2019-07-10T11:22:51.863 回答