R 相当于 python 的 os.getpid() 用于并行处理
R equivalent to python's os.getpid() for parallel processing
我经常在 python(多处理库)和 r(snow 等包)下使用并行处理。
我发现 python 中非常有用的一件事是能够使用实例的唯一标识符记录单个实例的进度,因此我可以跟踪,例如,是否启动了正确数量的实例并且 运行 适当地。
要在 python 中做到这一点,我只需使用 os.getpid()。
r中有类似的命令吗?我已经搜索过但没有找到。
例如,下面是 r 中的抽象并行代码示例,我希望实例日志文件在日志文件名中包含实例 ID(第 17 行),以及进程开始的时间:
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()
??pid
returns base::Sys.getpid
在结果列表顶部附近(将取决于您安装的软件包)。
详情见?Sys.getpid
。
我经常在 python(多处理库)和 r(snow 等包)下使用并行处理。 我发现 python 中非常有用的一件事是能够使用实例的唯一标识符记录单个实例的进度,因此我可以跟踪,例如,是否启动了正确数量的实例并且 运行 适当地。 要在 python 中做到这一点,我只需使用 os.getpid()。
r中有类似的命令吗?我已经搜索过但没有找到。
例如,下面是 r 中的抽象并行代码示例,我希望实例日志文件在日志文件名中包含实例 ID(第 17 行),以及进程开始的时间:
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()
??pid
returns base::Sys.getpid
在结果列表顶部附近(将取决于您安装的软件包)。
详情见?Sys.getpid
。