Calling python from rust threads #3499
-
I'm trying to understand how and if rust threading works with passing a python object to different rust threads and calling the python object's methods. I am not interested in bypassing the GIL, asyncio, or anything fancy. I just have an old school python object, which I instantiate once, and then I pass around to several threads, which use its methods. Exactly as I would do with pure python and the python threading module. It's up to the object to be thread-safe. Only now my threads are rust threads, my python object is a In pure python land this would work fine (with parallelism achieved through |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Generally speaking this works with PyO3 just the same where |
Beta Was this translation helpful? Give feedback.
No, same as
with_gil
,allow_threads
is just a safe Rust API around the samePyEval_Save/RestoreThread
API backingPy_BEGIN_ALLOW_THREADS
. I usedallow_threads
to keep the example short, but it does not matter whether Python or Rust or C code release the GIL insidewith_gil
(because that also just wraps the C API).