I need to perform R
OK, this can be done this way (caught it from some nice person who finally answered me on Rserve devel mailing list):
In the thread running the
eval supposed to be blocking or too long, and assuming Rserve is started:
private RConnection rEngine = null; private int rServePid = -1; //... // Keep an opened instance and store the related pid RConnection rconn = new RConnection(); this.rServePid = rconn.eval("Sys.getpid()").asInteger(); this.rEngine = rconn; LOG.info("Rserve: started instance with pid '" + this.rServePid + "'."); //... this.rEngine.eval("some consuming code...");
Which allows to keep track of the pid of the instance related to the said
eval (R privides
Then to stop / abort / cancel and since a simple
this.rEngine.close() will not stop the task being processed on server side, but only close the connection, we need to kill the targeted Rserve instance. This can be done by calling
tools::pskill() (or any other system call like possibly
kill -9 my_pid (UNIX*),
TASKKILL /PID my_pid /F (Windows), ..., depending on the platform), obviously from another thread than the one above (which is waiting for the "eval part" to return):
// Terminate. RConnection c2 = new RConnection(); // SIGTERM might not be understood everywhere: so using SIGKILL signal, as well. c2.eval("tools::pskill("+ this.rServePid + ")"); c2.eval("tools::pskill("+ this.rServePid + ", tools::SIGKILL)"); c2.close(); LOG.info("Rserve: terminated instance with pid '" + this.rServePid + "'.");
That one has the benefit to be plateform independent.
Hope this can help.