Lucas Fortini
Lucas Fortini

Reputation: 2480

Detect number of running r instances in windows within r

I have an r code I am creating that I would like to detect the number of running instances of R in windows so the script can choose whether or not to run a particular set of scripts (i.e., if there is already >2 instances of R running do X, else Y).

Is there a way to do this within R?

EDIT: Here is some info on the purpose as requested: I have a very long set of scripts for applying a bayesian network model using the catnet library for thousands of cases. This code processes and outputs results in a csv file for each case. Most of the parallel computing alternatives I have tried have not been ideal as they suppress a lot of the built-in notification of progress, hence I have been running a subset of the cases on different instances of R. I know this is somewhat antiquated, but it works for me, so I wanted a way to have the code subset the number of cases automatically based on the number of instances running. I do this right now by hand by opening multiple instances of Rscript in CMD opening slightly differently configured r files to get something like this: 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

EDIT2:

Thanks to the answers below, here is my implementation of what I call 'poorman's parallel computing in R: So if you have any long script that has to be applied to a long list of cases use the code below to break the long list into a number of sublists to be fed to each instance of 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)
}

To make this code run on multiple instances of r automatically in windows, just create a .bat file:

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

The timeout is there to give enough time for r to detect its own number of instances. Clicking on this .bat file will automatically open up numerous instances of r script, with each one taking on a particular subset of the cases you want to analyse, while still providing all of the progress of the running of the script in each window, like the image above. The cool thing about this approach is that you pretty much just have to slap on the automated list subsetting code before whichever iteration mechanism you are using in your code (loops, apply fx, etc). Then just fire the code using rcript using the .bat or manually and you are set.

Upvotes: 3

Views: 1283

Answers (2)

CHP
CHP

Reputation: 17189

You can do even shorter as below

length(grep("rstudio\\.exe", system("tasklist", intern = TRUE)))

Replace rstudio with any other Rscript or any other process name

Or even shorter

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

Upvotes: 4

Henrik
Henrik

Reputation: 14460

Actually it is easier than expected, as Windows comes with the nice function tasklist found here.

With it you can get all running processes from which you simply need to count the number of Rscript.exe instances (I use stringr here for string manipulations).

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

That should do the trick. (I only tried it with counting instances of Rgui.exe but that works fine.)

Upvotes: 8

Related Questions