# Using PyCall on different threads crashes my Julia REPL

**URL:** <https://discourse.julialang.org/t/using-pycall-on-different-threads-crashes-my-julia-repl/71266>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 10, 2021, 4:19pm UTC](https://discourse.julialang.org/t/using-pycall-on-different-threads-crashes-my-julia-repl/71266 "2021-11-10T16:19:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [November 10, 2021, 4:19pm UTC](https://discourse.julialang.org/t/using-pycall-on-different-threads-crashes-my-julia-repl/71266/1 "2021-11-10T16:19:53Z")

</div>

Hi all 🙂

the following two lines crashes my Julia REPL.

```julia
julia> using PyCall

julia> [Threads.@spawn py"sum(i for i in range(10_000_000))" for i in 1:10]

```

Anyone with the same experience? Does anyone know why?

Cheers

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 10, 2021, 5:21pm UTC](https://discourse.julialang.org/t/using-pycall-on-different-threads-crashes-my-julia-repl/71266/2 "2021-11-10T17:21:09Z")

</div>

> [@Impressium](#):
>
> Anyone with the same experience? Does anyone know why?

Python isn’t thread safe.

(Realize that PyCall is calling a single instance of the standard Python interpreter `libpython` that shares memory with the Julia process. This Python interpreter does not support concurrent calls. PyCall is _not_ launching separate `python` processes, which would be very slow (but thread-safe).)

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [November 10, 2021, 8:21pm UTC](https://discourse.julialang.org/t/using-pycall-on-different-threads-crashes-my-julia-repl/71266/3 "2021-11-10T20:21:36Z")

</div>

If each thread acquired and released the Global Interpreter Lock (GIL) then it should work. I’m not sure if PyCall exposes that functionality. Even if that works, like multithreading from Python itself, this will only accelerate I/O bound tasks.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 10, 2021, 9:28pm UTC](https://discourse.julialang.org/t/using-pycall-on-different-threads-crashes-my-julia-repl/71266/4 "2021-11-10T21:28:58Z")

</div>

> [@cjdoris](#):
>
> If each thread acquired and released the Global Interpreter Lock (GIL) then it should work. I’m not sure if PyCall exposes that functionality.

No, you’d have to `ccall` to [`PyEval_SaveThread ` and `PyEval_RestoreThread`](https://docs.python.org/3/c-api/init.html#releasing-the-gil-from-extension-code) yourself.
