卸载rJava和/或重新启动JVM [英] Unloading rJava and/or restarting JVM

查看:922
本文介绍了卸载rJava和/或重新启动JVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 rJava mcparallel 结合使用,但显然 JVM无法分叉。因此,需要为每个子进程启动一个单独的JVM实例,例如:

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)
}))

然而,在我的情况下,问题是JVM已经在(主)父进程中启动了。这使得在子进程中无法使用 rJava

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)
}))

所以我真正需要的是一种关闭/杀死和重启的方法子进程中的JVM。只需 detach(package:rJava,unload = TRUE)似乎无法解决问题。 force.init 参数似乎不会导致重启:

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)
}))

有什么方法可以强制关闭/杀死JVM以便在子进程中重新启动它?

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

推荐答案

有一种方法可以并行使用rJava运行表达式在运行并行流程以获取和汇总所有结果 BEFORE 您在主进程中加载​​rJava库。由于主R进程尚未启动jvm,因此java在每个子进程中启动,并且此特定实例也将与子进程一起死亡。

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

这篇关于卸载rJava和/或重新启动JVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆