Wil*_*ren 5 console cmd r multiprocessing snow
我正在使用 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)
Run Code Online (Sandbox Code Playgroud)
如果您通过终端、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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
308 次 |
| 最近记录: |