JuliaInterop / JuliaCall

Embed Julia in R
https://non-contradiction.github.io/JuliaCall/index.html
Other
267 stars 36 forks source link

Question: Stop Julia session #126

Open instantkaffee opened 4 years ago

instantkaffee commented 4 years ago

No issue, just a simple question, maybe an idea for package extension:

Does it make sense to implement a function which terminates a Julia session? Are the ways to do this already ?

Thx

Non-Contradiction commented 4 years ago

Currently there seems to be no way to stop a julia session. Are there any specific use case for this? Thx for the feedback!

jjlynch2 commented 4 years ago

This would be useful for my applications that start Julia processes that can run for days at a time. It would be nice to have a way to kill the process without having to close R.

instantkaffee commented 4 years ago

Currently there seems to be no way to stop a julia session. Are there any specific use case for this? Thx for the feedback!

My usecase: I am iterating over many cycles via purrr everytime starting a new Julia instance. I fear this is not memory efficient.

Non-Contradiction commented 4 years ago

@instantkaffee Unless you are starting a new R instance, otherwise JuliaCall will (nearly) always use the existing Julia instance instead of starting a new one.

Non-Contradiction commented 4 years ago

@jjlynch2 There is still no general solution to stop Julia process in JuliaCall. One idea is to insert R callbacks into the julia function or statement you are executing. For example,

library(JuliaCall)
julia_setup()

jlongcalc <- julia_eval('longcalc() = begin i=0; while(true) i = i+1; RCall.rcall(:print, i); end; i; end;')

In the example, jlongcalc is a Julia function that executes forever by using while(true). However, in the function, an R callback is inserted RCall.rcall(:print, i);. So when the Julia code executes the R callback part, it will give the control back to R, and you can stop it just like stopping any R code. For example, in RStudio, if you jlongcalc(), and click the stop button, then the execution of the function jlongcalc() will be stopped just like any R code. PS: A similar idea is also used in C/RCpp when you have long calculations: if you want the C/Cpp code to respond to R stopping signal, then you need to insert some R callbacks into the C/RCpp code.

One disadvantage for this method is that it is very hard to become a general solution. If JuliaCall automatically inserts R callbacks into users' Julia code, it will make the users' Julia code running slower than it should be.