# Calling Julia from Python on thread

**URL:** <https://discourse.julialang.org/t/calling-julia-from-python-on-thread/43187>\
**Category:** General Usage\
**Tags:** multithreading, python\
**Created:** [July 16, 2020, 4:08pm UTC](https://discourse.julialang.org/t/calling-julia-from-python-on-thread/43187 "2020-07-16T16:08:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![james\_p\_d](https://avatars.discourse-cdn.com/v4/letter/j/90db22/32.png) [@james\_p\_d](https://discourse.julialang.org/u/james_p_d)\
**Post date:** [July 16, 2020, 4:08pm UTC](https://discourse.julialang.org/t/calling-julia-from-python-on-thread/43187/1 "2020-07-16T16:08:44Z")

</div>

I’ve been playing around with calling Julia functions from Python which has all worked fine until I try and call from a thread. To illustrate, here is a short example:

**Julia code:**

```julia
function add(x, y)
    return x + y
end

```

**Python code:**

```julia
import threading
import time
import julia
from julia import Main # Move these two lines to add_numbers_from_julia() to call from thread
Main.include('library.jl') # ..but it will only work *once* :|

def add_numbers_from_julia():
    z = Main.add(4, 5)
    print(f"result = {z}")

def main():
    i = -1
    while i != 0:
        print("Options:")
        print("0 - Exit")
        print("1 - Add 4 and 5")
        print("2 - Add 4 and 5 from thread")

        i = int(input())

        if i == 1:
            add_numbers_from_julia()
        elif i == 2:
            thread = threading.Thread(target = add_numbers_from_julia, args = ())
            thread.start()
            time.sleep(5)
        print()
            

if __name__ == " __main__":
    main()

```

In the above code, if the user chooses option `1` , we call the Julia `add(x,y)` function on the current thread and it all works fine.

If the user chooses option `2` we attempt to call `add(x,y)` from a thread, which causes an access violation. If we remove the `from julia import Main` and `Main.include('library.jl')` lines from the top of the file and put them into the `add_numbers_from_julia()` function, then we can successfully call `add(x,y)`_ **but only once** _ . Attempting to hit `2` again will cause another access violation.

Can someone tell me whats going wrong and where I should be importing the Julia code if I want to call it from a thread?

Many TIA

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [July 16, 2020, 6:38pm UTC](https://discourse.julialang.org/t/calling-julia-from-python-on-thread/43187/2 "2020-07-16T18:38:31Z")

</div>

I encountered a similar issue some time ago. PyJulia is not thread-safe, see here:

[https://github.com/dask/dask/issues/5732](https://github.com/dask/dask/issues/5732)

However, it should work if you use Multiprocessing on Python-side.

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [July 17, 2020, 1:13am UTC](https://discourse.julialang.org/t/calling-julia-from-python-on-thread/43187/3 "2020-07-17T01:13:39Z")

</div>

> However, it should work if you use Multiprocessing on Python-side.

Need to be careful here because the default multiprocessing mode forks the process, which tends to break things in a multithreaded program which does not expect a fork. Use `multiprocessing.set_start_method("spawn")` to launch independent processes, which should safe.
