# Run multiple python instances with pycall in different threads

**URL:** <https://discourse.julialang.org/t/run-multiple-python-instances-with-pycall-in-different-threads/78399>\
**Category:** General Usage\
**Tags:** pycall, parallel\
**Created:** [March 24, 2022, 2:38pm UTC](https://discourse.julialang.org/t/run-multiple-python-instances-with-pycall-in-different-threads/78399 "2022-03-24T14:38:41Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [March 24, 2022, 3:41pm UTC](https://discourse.julialang.org/t/run-multiple-python-instances-with-pycall-in-different-threads/78399/2 "2022-03-24T15:41:40Z")

</div>

I don’t have a working PyCall to hand, but with PythonCall the following works fine, and I’d expect PyCall to work just the same:

```julia-repl
julia> @everywhere using PythonCall

julia> @time pmap(1:4) do i
           __main__ , time = pyimport(" __main__", "time")
           time.sleep(4 - i)
           __main__.foo = myid()
           time.sleep(i)
           i => __main__.foo
       end
  4.284934 seconds (268.97 k allocations: 14.611 MiB, 3.89% compilation time)
4-element Vector{Pair{Int64, Py}}:
 1 => 5
 2 => 2
 3 => 3
 4 => 4

```

In this code, each iteration sleeps for a total of 4 seconds, but sets ` __main__.foo` to the worker ID at a different time, then returns ` __main__.foo` at the end. The total run-time is just over 4 seconds so they are indeed in parallel. Since they return different numbers, they are running separate interpreters (if they were running the same interpreter, ` __main__.foo` would be the same number in all workers.)

---

_[View the full topic](https://discourse.julialang.org/t/run-multiple-python-instances-with-pycall-in-different-threads/78399)._
