Unloading rJava and/or restarting JVM
Asked Answered
C

1

12

I would like to use rJava in combination with mcparallel but obviously the JVM cannot be forked. Therefore a separate JVM instance needs to be initiated for each child process, e.g:

library(rJava)
library(parallel)
myfile <-  system.file("tests", "test_import.xlsx", package = "xlsx")

#This works:
mccollect(mcparallel({
  #Automatically initiates JVM in child
  xlsx::read.xlsx(myfile, 1)
}))

However the problem in my case is that the JVM has already been initiated in the (main) parent process as well. This makes it impossible to use rJava in the child process:

#init JVM in parent
.jinit()

#Doesn't work anymore
mccollect(mcparallel({
  xlsx::read.xlsx(myfile, 1)
}))

So what I really need is a way to shutdown/kill and restart the JVM in the child process. Simply detach("package:rJava", unload = TRUE) doesn't seem to do the trick. The force.init parameter doesn't seem to result in a restart either:

#Also doesn't work:
.jinit()
mccollect(mcparallel({
  .jinit(force.init = TRUE)
  xlsx::read.xlsx(myfile, 1)
}))

Is there some way I can forcefully shutdown/kill the JVM in order to reinitiate it in the child process?

Cute answered 21/6, 2014 at 1:16 Comment(2)
When I had a similar need in the past, I ended up implementing a main method in the Java code to allow execution from R as an external program using 'system'.Tucana
Just for the record, a github issue was opened for this: github.com/s-u/rJava/issues/25Tucana
R
1

There is a way to run expressions using rJava in parallel based on running the parallel processes to get and assemble all results BEFORE you load the rJava library in the main process. As the main R process has not initiated jvm then java is started in each single subprocess and this particular instance will die together with subprocess as well.

# Rsession started
library(parallel)
myfile <-  system.file("tests", "test_import.xlsx", package = "xlsx")
e <- expression({
require(rJava)
require(xlsx)
read.xlsx(myfile, 1)
})
p <- mcparallel(e)
q <- mcparallel(e)
pq <- mccollect(list(p, q))

# again to check reproducibility
p <- mcparallel(e)
q <- mcparallel(e)
pq2 <- mccollect(list(p, q))
identical(unname(pq),unname(pq2))

# see the result if it is the right content and not tryerr
pq

# now the main continues ...
# and if necessary even load rJava
Rationale answered 12/7, 2014 at 11:54 Comment(1)
This doesn't solve the problem. In my application I do need rJava in the parent process.Cute

© 2022 - 2024 — McMap. All rights reserved.