Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在r中检测windows中运行的r实例数_R - Fatal编程技术网

在r中检测windows中运行的r实例数

在r中检测windows中运行的r实例数,r,R,我有一个我正在创建的r代码,我想检测windows中r的运行实例数,以便脚本可以选择是否运行一组特定的脚本(即,如果已经有>2个r实例运行do X,否则为Y) 有没有办法在R内做到这一点 编辑: 以下是有关要求用途的一些信息: 我有一套非常长的脚本,用于在数千个案例中使用catnet库应用贝叶斯网络模型。此代码处理并输出每个案例的csv文件结果。我尝试过的大多数并行计算备选方案并不理想,因为它们抑制了许多内置的进度通知,因此我在不同的R实例上运行了一部分案例。我知道这有点过时,但它对我有效,因此

我有一个我正在创建的r代码,我想检测windows中r的运行实例数,以便脚本可以选择是否运行一组特定的脚本(即,如果已经有>2个r实例运行do X,否则为Y)

有没有办法在R内做到这一点

编辑: 以下是有关要求用途的一些信息: 我有一套非常长的脚本,用于在数千个案例中使用catnet库应用贝叶斯网络模型。此代码处理并输出每个案例的csv文件结果。我尝试过的大多数并行计算备选方案并不理想,因为它们抑制了许多内置的进度通知,因此我在不同的R实例上运行了一部分案例。我知道这有点过时,但它对我有效,因此,我想找到一种方法,让代码子集根据运行的实例数自动计算案例数。 我现在手动执行此操作,方法是在CMD中打开多个Rscript实例,然后打开配置稍有不同的r文件,得到如下结果:

编辑2:

多亏了下面的答案,以下是我在R中实现的所谓“poorman的并行计算”: 因此,如果您有任何长脚本必须应用于一长串案例,请使用下面的代码将长串列表分解为若干个子列表,以提供给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)
}
超时是为了给r足够的时间来检测其自身的实例数。
单击此.bat文件将自动打开r脚本的多个实例,每个实例都包含您要分析的案例的特定子集,同时仍提供每个窗口中脚本运行的所有进度,如上图所示。这种方法很酷的一点是,在代码中使用的任何迭代机制(循环、应用fx等)之前,您几乎只需要在自动列表子集代码上加上一记耳光。然后,只需使用.bat或手动使用rccript启动代码,就可以进行设置。

实际上,这比预期的要容易,因为Windows附带了一个很好的功能
任务列表

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

require(stringr)

progs你可以做得更短,如下所示

length(grep(“rstudio\\.exe”,系统(“任务列表”,intern=TRUE))

用任何其他
Rscript
或任何其他进程名称替换
rstudio

甚至更短


length(system('tasklist/FI“IMAGENAME eq Rscript.exe',intern=TRUE))-3

可能有一种方法可以做到这一点,但您实际想要实现什么?这似乎是一种代码味道。我同意@JackManey的说法,您可能不想这样做,但如果您真的想这样做,您可以尝试使用文件系统作为信号量,即读/写文件作为跟踪整个系统发生的事情的一种方式。@eddi-唯一的问题是可能在两个或多个并发会话之间进行覆盖。必须使用某种类型的文件锁(如果R会话在两个或多个系统上,这将使这种方法无效)。@JackManey-这个问题很容易解决-不要写入同一个文件,每个会话都有一个文件。@Lucas-这听起来像是脚本管理器/启动器的工作(这将知道谁在运行什么)而不是针对特定的脚本本身。回答得很好!一个简短的解决方案完全符合我的要求
#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)
}
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
require(stringr)
progs <- system("tasklist", intern = TRUE)
progs <- vapply(str_split(progs, "[[:space:]]"), "[[", "", i = 1)
sum(progs == "Rscript.exe")