# Julia crashed when using PyCall in multiple threads

**URL:** <https://discourse.julialang.org/t/julia-crashed-when-using-pycall-in-multiple-threads/111004>\
**Category:** General Usage\
**Tags:** pycall\
**Created:** [March 1, 2024, 9:20am UTC](https://discourse.julialang.org/t/julia-crashed-when-using-pycall-in-multiple-threads/111004 "2024-03-01T09:20:44Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)\
**Post date:** [June 12, 2024, 11:06pm UTC](https://discourse.julialang.org/t/julia-crashed-when-using-pycall-in-multiple-threads/111004/9 "2024-06-12T23:06:38Z")

</div>

Stumbled across this very problem and after some searching found the following solution:

```julia
using PythonCall
using ThreadPools

macro pythread(expr)
    quote
        fetch(@tspawnat 1 begin
            $(esc(expr))
        end)
    end
end

function babel_convert(input, informat = "cdxml", outformat = "mol")
    @pythread begin
        ob = pyimport("openbabel.openbabel")
        conv = ob.OBConversion()
        conv.SetInAndOutFormats(informat, outformat)
        
        mol = ob.OBMol()
        conv.ReadString(mol, input)
        
        if ! Bool(mol.Has2D()) && ! Bool(mol.Has3D())
            pgen = ob.OBOp.FindType("gen2D")
            pgen !== nothing && pgen.Do(mol)
        end

        pyconvert(String, conv.WriteString(mol))
    end
end

PythonCall.GC.disable()
Threads.@threads for i in 1:10000
    babel_convert(cdxml)
end
PythonCall.GC.enable()

```

---

_[View the full topic](https://discourse.julialang.org/t/julia-crashed-when-using-pycall-in-multiple-threads/111004)._
