在 clusterApply 或 clusterMap 中将进度打印到 windows cmd
Print progress to windows cmd within clusterApply or clusterMap
我正在使用 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)
如果您通过终端、cmd 或 powershell 运行 它,您可以添加一个额外的 system
或 shell
调用来打印您的字符串。例如: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)
我正在使用 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)
如果您通过终端、cmd 或 powershell 运行 它,您可以添加一个额外的 system
或 shell
调用来打印您的字符串。例如: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)