【发布时间】:2015-12-28 21:36:37
【问题描述】:
我经常在 python(多处理库)和 r(snow 等包)下使用并行处理。 我发现在 python 中非常有用的一件事是能够使用实例的唯一标识符记录单个实例的进度,因此我可以跟踪,例如,是否启动了正确数量的实例并正常运行。 要在 python 中做到这一点,我只需使用 os.getpid()。
r中有类似的命令吗?我已经搜索但没有找到。
例如,下面是 r 中抽象并行代码的示例,我希望实例日志文件在日志文件名(第 17 行)中包含实例 ID,以及进程启动的时间:
rm(list = ls()) #remove all past worksheet variables
wd="D:/temp/" #location for log files
setwd(wd)
n_spp=30
spp_nmS=paste0("sp_",c(1:n_spp))
#sp_nm=spp_nmS[1]
library(snowfall)
#stop sinks
sink.reset <- function(){
for(i in seq_len(sink.number())){
sink(NULL)
}
}
sp_parallel_run=function(sp_nm){
file_nm=paste0(wd,sp_nm,"_log_",format(Sys.time(), "%a %b %d %H%M%S"), ".txt")
con=file(file_nm, open="wt")
sink(con)
cat('\n', 'Started on ', date(), '\n')
ptm0 <- proc.time()
#start code
sp_nm
Sys.sleep(10)
#end code
ptm1=proc.time() - ptm0
jnk=as.numeric(ptm1[3])
cat('\n','It took ', jnk, "seconds to model", sp_nm)
sink.reset()
close(con)
}
sfInit( parallel=TRUE, cpus=as.integer(Sys.getenv('NUMBER_OF_PROCESSORS'))) #
sfExportAll()
sfLapply(x=spp_nmS, fun=sp_parallel_run)
sfRemoveAll()
sfStop()
【问题讨论】:
标签: python r parallel-processing multiprocessing