r - 检测r内windows中正在运行的r个实例的数量

标签 r

我有一个正在创建的 r 代码,我想检测 Windows 中正在运行的 R 实例的数量,以便脚本可以选择是否运行一组特定的脚本(即,如果已经有 >2 个 R 实例运行做X,否则Y)。
有没有办法在 R 中做到这一点?
编辑:
以下是有关目的的一些信息:
我有很长的脚本集,用于使用 catnet 库为数千个案例应用贝叶斯网络模型。此代码处理并将结果输出到每个案例的 csv 文件中。我尝试过的大多数并行计算替代方案并不理想,因为它们抑制了很多内置的进度通知,因此我一直在不同的 R 实例上运行案例的子集。我知道这有点过时,但它对我有用,所以我想要一种方法,让代码子集根据运行的实例数量自动确定案例数量。
我现在通过在 CMD 中打开多个 Rscript 实例来手动执行此操作,打开配置略有不同的 r 文件以获得如下内容:
enter image description here

cd "Y:\code\BN_code"
START "" "C:\Program Files\R\R-3.0.0\bin\x64\Rscript.exe" "process spp data3.r" /b
START "" "C:\Program Files\R\R-3.0.0\bin\x64\Rscript.exe" "process spp data3_T1.r" /b
START "" "C:\Program Files\R\R-3.0.0\bin\x64\Rscript.exe" "process spp data3_T2.r" /b
START "" "C:\Program Files\R\R-3.0.0\bin\x64\Rscript.exe" "process spp data3_T3.r" /b
编辑2:
多亏了下面的答案,这是我在 R 中所谓的“穷人的并行计算”的实现:
因此,如果您有任何必须应用于一长串案例的长脚本,请使用以下代码将长列表分解为多个子列表,以提供给 rscript 的每个实例:
#the cases that I need to apply my code to:
splist=c("sp01", "sp02", "sp03", "sp04", "sp05", "sp06", "sp07", "sp08", "sp09", "sp010", "sp11", "sp12", 
         "sp013", "sp014", "sp015", "sp16", "sp17", "sp018", "sp19", "sp20", "sp21", "sp22", "sp23", "sp24")
###automatic subsetting of cases based on number of running instances of r script:
cpucores=as.integer(Sys.getenv('NUMBER_OF_PROCESSORS'))
n_instances=length(system('tasklist /FI "IMAGENAME eq Rscript.exe" ', intern = TRUE))-3
jnk=length(system('tasklist /FI "IMAGENAME eq rstudio.exe" ', intern = TRUE))-3
if (jnk>0)rstudiorun=TRUE else rstudiorun=FALSE 

if (!rstudiorun & n_instances>0 & cpucores>1){ #if code is being run from rscript and 
#not from rstudio and there is more than one core available
  jnkn=length(splist)
  jnk=seq(1,jnkn,round(jnkn/cpucores,0))
  jnk=c(jnk,jnkn)
  splist=splist[jnk[n_instances]:jnk[n_instances+1]]
}
###end automatic subsetting of cases

#perform your script on subset of list of cases:
for(sp in splist){
  ptm0 <- proc.time()
  Sys.sleep(6)  
  ptm1=proc.time() - ptm0
  jnk=as.numeric(ptm1[3])
  cat('\n','It took ', jnk, "seconds to do species", sp)
}
要使此代码在 Windows 中自动在 r 的多个实例上运行,只需创建一个 .bat 文件:
cd "C:\Users\lfortini\code\misc code\misc r code"
START "" "C:\Program Files\R\R-3.0.0\bin\x64\Rscript.exe" "rscript_multiple_instances.r" /b
timeout 10
START "" "C:\Program Files\R\R-3.0.0\bin\x64\Rscript.exe" "rscript_multiple_instances.r" /b
timeout 10
START "" "C:\Program Files\R\R-3.0.0\bin\x64\Rscript.exe" "rscript_multiple_instances.r" /b
timeout 10
START "" "C:\Program Files\R\R-3.0.0\bin\x64\Rscript.exe" "rscript_multiple_instances.r" /b
exit
超时是为了给 r 足够的时间来检测它自己的实例数。
单击此 .bat 文件将自动打开 r 脚本的多个实例,每个实例都会处理您要分析的案例的特定子集,同时仍然提供每个窗口中脚本运行的所有进度,例如上图。这种方法很酷的一点是,您几乎只需要在代码中使用的任何迭代机制(循环、应用 fx 等)之前添加自动列表子集代码。然后只需使用 .bat 或手动使用 rcript 触发代码,就可以了。

最佳答案

其实比想象的要容易,因为Windows自带了不错的功能tasklist找到 here .

有了它,您可以获得所有正在运行的进程,您只需计算 Rscript.exe 的数量即可。实例(我在这里使用 stringr 进行字符串操作)。

require(stringr)
progs <- system("tasklist", intern = TRUE)
progs <- vapply(str_split(progs, "[[:space:]]"), "[[", "", i = 1)
sum(progs == "Rscript.exe")

这应该够了吧。 (我只尝试计算 Rgui.exe 的实例,但效果很好。)

关于r - 检测r内windows中正在运行的r个实例的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15935931/

相关文章:

r - 隐藏 Shiny 的输出

r - 绘制变量随时间的分布 - 累积加法

Rstudio 不使用 knit Word 制作绘图

r - 在 R 编程中对数据帧中的列中的值进行计数

rbindlist data.frames 的列表列并选择唯一值

r- 使用 purrr::map 时存储警告消息而不丢弃结果

从 R 中的直方图中的条形中删除边框

r - 如何按组绘制加权平均值?

r - 为什么在 map() 调用中使用 with() 作为函数在这个例子中不起作用?

r - 动态扩展引用类时如何控制继承