I’m trying to create a micro service in C++ that calls julia at each new request. In the first call it works just fine but it crashes with a signal (11): Segmentation fault
at the second call. I’ve tried initializing the julia runtime with jl_init
, jl_init_with_image
and init_julia
but the problem remains the same. After every call I exit the julia runtime using jl_atexit_hook(0);
. Is there any limitation in starting the julia runtime sequentially (I don’t need multiple julia running at the same time)? Or I am missing something else?
You only need to initialize the runtime once - it’s supposed to be kept around between function invocations. I suggest you do that outside of your request calling logic, since calling jl_init
more than once effectively means running julia multiple times at the same time (in the same process - which will break).
Thanks for the suggestion, I’ve tried that approach without success. If jl_init
happens outside of the function invocation, any call to jl_eval_string
raises a signal (11): Segmentation fault
. It seems that there is some scope issue happening. Do I need to somehow pass some reference so julia can be called inside a function given that the initialization happended outside?
That doesn’t sound right Do you have a minimal example showing that behavior?
I can understand that happening if the calls are done from different threads, but not otherwise. You can see an embedding example in C with jl_init
in a separate function from other Julia calls in GitHub - GunnarFarneback/DynamicallyLoadedEmbedding.jl: Embed Julia with dynamical loading of libjulia at runtime.
Both of you are right. It is working on the minimal example below but not on my actual application.
#include <julia.h>
#include <iostream>
#include <string>
#include <stdio.h>
class JuliaClass
{
public:
void initJulia()
{
jl_init();
std::cout << "Started julia runtime.\n";
}
void myMethod()
{
jl_eval_string("print(sqrt(2.0))");
std::cout << "\nCalled julia.\n";
}
void finishedJulia()
{
jl_atexit_hook(0);
std::cout << "Finished julia runtime.\n";
}
};
int main() {
JuliaClass myObj;
myObj.initJulia();
myObj.myMethod();
myObj.myMethod();
myObj.finishedJulia();
return 0;
}
I’m using gRPC on my actual application and I think that is creating additional threads on the server side that makes it problematic.