# Initialize the julia runtime more than once within C++

**URL:** https://discourse.julialang.org/t/initialize-the-julia-runtime-more-than-once-within-c/82892
**Category:** Performance
**Tags:** embedding, c
**Created:** [June 16, 2022, 6:35pm UTC](https://discourse.julialang.org/t/initialize-the-julia-runtime-more-than-once-within-c/82892 "2022-06-16T18:35:06Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![MarioSouto](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mariosouto/32/36673_2.png) [@MarioSouto](https://discourse.julialang.org/u/MarioSouto)
#### Post date: [June 16, 2022, 6:35pm UTC](https://discourse.julialang.org/t/initialize-the-julia-runtime-more-than-once-within-c/82892/1 "2022-06-16T18:35:06Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [June 16, 2022, 7:17pm UTC](https://discourse.julialang.org/t/initialize-the-julia-runtime-more-than-once-within-c/82892/2 "2022-06-16T19:17:29Z")

</div>

> [@MarioSouto](#):
>
> 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).

---

<div class="post-metadata">

### Author: ![MarioSouto](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mariosouto/32/36673_2.png) [@MarioSouto](https://discourse.julialang.org/u/MarioSouto)
#### Post date: [June 17, 2022, 1:28am UTC](https://discourse.julialang.org/t/initialize-the-julia-runtime-more-than-once-within-c/82892/3 "2022-06-17T01:28:29Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [June 17, 2022, 4:43am UTC](https://discourse.julialang.org/t/initialize-the-julia-runtime-more-than-once-within-c/82892/4 "2022-06-17T04:43:00Z")

</div>

> [@MarioSouto](#):
>
> If `jl_init` happens outside of the function invocation, any call to `jl_eval_string` raises a `signal (11): Segmentation fault`.

That doesn’t sound right 🤔 Do you have a minimal example showing that behavior?

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [June 17, 2022, 5:44am UTC](https://discourse.julialang.org/t/initialize-the-julia-runtime-more-than-once-within-c/82892/5 "2022-06-17T05:44:02Z")

</div>

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.](https://github.com/GunnarFarneback/DynamicallyLoadedEmbedding.jl)

---

<div class="post-metadata">

### Author: ![MarioSouto](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mariosouto/32/36673_2.png) [@MarioSouto](https://discourse.julialang.org/u/MarioSouto)
#### Post date: [June 18, 2022, 7:49pm UTC](https://discourse.julialang.org/t/initialize-the-julia-runtime-more-than-once-within-c/82892/6 "2022-06-18T19:49:34Z")

</div>

Both of you are right. It is working on the minimal example below but not on my actual application.

```julia
#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.
