Skip to content

Commit 4a1ee78

Browse files
authored
GIL functions for genuine multi-threading (#535)
* slightly more thread safe gc * use Channel not Vector and make disable/enable a no-op * document GCHook * cannot lock channels on julia 1.6 * revert to using a vector for the queue * restore test script * combine queue into a single item * prefer Fix2 over anonymous function * update docs * test multithreaded * test gc from python * add gc tests * fix test * add deprecation warnings * safer locking (plus explanatory comments) * ref of weakref * SpinLock -> ReentrantLock * SpinLock -> ReentrantLock * add PythonCall.GIL * add tests for PythonCall.GIL * add GIL to release notes * add GIL release tests from Python * typo: testset -> testitem * delete redundant test * remove out of date comment * comment erroneous test * re-enable commented test * adds AnyValue._jl_call_nogil * add RawValue._jl_call_nogil * add docstrings * add warnings about the GIL to docstrings * add reference docstrings * remove big pycall comparison and move pycall help to faq * document new threading features * update release notes * clarification * rename GIL.release to GIL.unlock and use lock/unlock terminology consistently --------- Co-authored-by: Christopher Doris <github.com/cjdoris>
1 parent bcd2bbb commit 4a1ee78

17 files changed

+416
-105
lines changed

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ In this example we use the Python module JuliaCall from an IPython notebook to t
4040

4141
## What about PyCall?
4242

43-
The existing package [PyCall](https://github.com/JuliaPy/PyCall.jl) is another similar interface to Python. Here we note some key differences, but a more detailed comparison is in the documentation.
43+
The existing package [PyCall](https://github.com/JuliaPy/PyCall.jl) is another similar interface to Python. Here we note some key differences:.
4444
- PythonCall supports a wider range of conversions between Julia and Python, and the conversion mechanism is extensible.
4545
- PythonCall by default never copies mutable objects when converting, but instead directly wraps the mutable object. This means that modifying the converted object modifies the original, and conversion is faster.
4646
- PythonCall does not usually automatically convert results to Julia values, but leaves them as Python objects. This makes it easier to do Pythonic things with these objects (e.g. accessing methods) and is type-stable.
47-
- PythonCall installs dependencies into a separate Conda environment for each Julia project. This means each Julia project can have an isolated set of Python dependencies.
48-
- PythonCall supports Julia 1.6.1+ and Python 3.8+ whereas PyCall supports Julia 0.7+ and Python 2.7+.
47+
- PythonCall installs dependencies into a separate Conda environment for each Julia project using [CondaPkg](https://github.com/JuliaPy/CondaPkg.jl). This means each Julia project can have an isolated set of Python dependencies.

docs/make.jl

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ makedocs(
1919
],
2020
"compat.md",
2121
"faq.md",
22-
"pycall.md",
2322
"releasenotes.md",
2423
],
2524
)

docs/src/faq.md

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
# FAQ & Troubleshooting
22

3-
## Is PythonCall/JuliaCall thread safe?
3+
## Can I use PythonCall and PyCall together?
4+
5+
Yes, you can use both PyCall and PythonCall in the same Julia session. This is platform-dependent:
6+
- On most systems the Python interpreter used by PythonCall and PyCall must be the same (see below).
7+
- On Windows it appears to be possible for PythonCall and PyCall to use different interpreters.
8+
9+
To force PythonCall to use the same Python interpreter as PyCall, set the environment variable [`JULIA_PYTHONCALL_EXE`](@ref pythoncall-config) to `"@PyCall"`. Note that this will opt out of automatic dependency management using CondaPkg.
410

5-
No.
11+
Alternatively, to force PyCall to use the same interpreter as PythonCall, set the environment variable `PYTHON` to [`PythonCall.python_executable_path()`](@ref) and then `Pkg.build("PyCall")`. You will need to do this each time you change project, because PythonCall by default uses a different Python for each project.
12+
13+
## Is PythonCall/JuliaCall thread safe?
614

7-
However it is safe to use PythonCall with Julia with multiple threads, provided you only
8-
call Python code from the first thread. (Before v0.9.22, tricks such as disabling the
9-
garbage collector were required.)
15+
Yes, as of v0.9.22, provided you handle the GIL correctly. See the guides for
16+
[PythonCall](@ref jl-multi-threading) and [JuliaCall](@ref py-multi-threading).
1017

11-
From Python, to use JuliaCall with multiple threads you probably need to set
12-
[`PYTHON_JULIACALL_HANDLE_SIGNALS=yes`](@ref julia-config) before importing JuliaCall.
13-
This is because Julia intentionally causes segmentation faults as part of the GC
14-
safepoint mechanism. If unhandled, these segfaults will result in termination of the
15-
process. This is equivalent to starting julia with `julia --handle-signals=yes`, the
16-
default behavior in Julia. See discussion
17-
[here](https://github.com/JuliaPy/PythonCall.jl/issues/219#issuecomment-1605087024)
18-
for more information.
18+
Before, tricks such as disabling the garbage collector were required. See the
19+
[old docs](https://juliapy.github.io/PythonCall.jl/v0.9.21/faq/#Is-PythonCall/JuliaCall-thread-safe?).
1920

2021
Related issues:
2122
[#201](https://github.com/JuliaPy/PythonCall.jl/issues/201),

docs/src/juliacall-reference.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# JuliaCall API Reference
1+
# [JuliaCall API Reference](@id jl-reference)
22

33
## Constants
44

@@ -93,8 +93,9 @@ replaced with `!!`.
9393
9494
###### Members
9595
- `_jl_raw()`: Convert to a [`RawValue`](#juliacall.RawValue). (See also [`pyjlraw`](@ref).)
96-
- `_jl_display()`: Display the object using Julia's display mechanism.
97-
- `_jl_help()`: Display help for the object.
96+
- `_jl_display(mime=None)`: Display the object using Julia's display mechanism.
97+
- `_jl_help(mime=None)`: Display help for the object.
98+
- `_jl_call_nogil(*args, **kwargs)`: Call this with the GIL disabled.
9899
`````
99100

100101
`````@customdoc
@@ -217,4 +218,5 @@ single tuple, it will need to be wrapped in another tuple.
217218
###### Members
218219
- `_jl_any()`: Convert to a [`AnyValue`](#juliacall.AnyValue) (or subclass). (See also
219220
[`pyjl`](@ref).)
221+
- `_jl_call_nogil(*args, **kwargs)`: Call this with the GIL disabled.
220222
`````

docs/src/juliacall.md

+76
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,79 @@ be configured in two ways:
124124
| `-X juliacall-threads=<N\|auto>` | `PYTHON_JULIACALL_THREADS=<N\|auto>` | Launch N threads. |
125125
| `-X juliacall-warn-overwrite=<yes\|no>` | `PYTHON_JULIACALL_WARN_OVERWRITE=<yes\|no>` | Enable or disable method overwrite warnings. |
126126
| `-X juliacall-autoload-ipython-extension=<yes\|no>` | `PYTHON_JULIACALL_AUTOLOAD_IPYTHON_EXTENSION=<yes\|no>` | Enable or disable IPython extension autoloading. |
127+
128+
## [Multi-threading](@id py-multi-threading)
129+
130+
From v0.9.22, JuliaCall supports multi-threading in Julia and/or Python, with some
131+
caveats.
132+
133+
Most importantly, you can only call Python code while Python's
134+
[Global Interpreter Lock (GIL)](https://docs.python.org/3/glossary.html#term-global-interpreter-lock)
135+
is locked by the current thread. You can use JuliaCall from any Python thread, and the GIL
136+
will be locked whenever any JuliaCall function is used. However, to leverage the benefits
137+
of multi-threading, you can unlock the GIL while executing any Julia code that does not
138+
interact with Python.
139+
140+
The simplest way to do this is using the `_jl_call_nogil` method on Julia functions to
141+
call the function with the GIL unlocked.
142+
143+
```python
144+
from concurrent.futures import ThreadPoolExecutor, wait
145+
from juliacall import Main as jl
146+
pool = ThreadPoolExecutor(4)
147+
fs = [pool.submit(jl.Libc.systemsleep._jl_call_nogil, 5) for _ in range(4)]
148+
wait(fs)
149+
```
150+
151+
In the above example, we call `Libc.systemsleep(5)` on four threads. Because we
152+
called it with `_jl_call_nogil`, the GIL was unlocked, allowing the threads to run in
153+
parallel, taking about 5 seconds in total.
154+
155+
If we did not use `_jl_call_nogil` (i.e. if we did `pool.submit(jl.Libc.systemsleep, 5)`)
156+
then the above code will take 20 seconds because the sleeps run one after another.
157+
158+
It is very important that any function called with `_jl_call_nogil` does not interact
159+
with Python at all unless it re-locks the GIL first, such as by using
160+
[PythonCall.GIL.@lock](@ref).
161+
162+
You can also use [multi-threading from Julia](@ref jl-multi-threading).
163+
164+
### Caveat: Julia's task scheduler
165+
166+
If you try the above example with a Julia function that yields to the task scheduler,
167+
such as `sleep` instead of `Libc.systemsleep`, then you will likely experience a hang.
168+
169+
In this case, you need to yield back to Julia's scheduler periodically to allow the task
170+
to continue. You can use the following pattern instead of `wait(fs)`:
171+
```python
172+
jl_yield = getattr(jl, "yield")
173+
while True:
174+
# yield to Julia's task scheduler
175+
jl_yield()
176+
# wait for up to 0.1 seconds for the threads to finish
177+
state = wait(fs, timeout=0.1)
178+
# if they finished then stop otherwise try again
179+
if not state.not_done:
180+
break
181+
```
182+
183+
Set the `timeout` parameter smaller to let Julia's scheduler cycle more frequently.
184+
185+
Future versions of JuliaCall may provide tooling to make this simpler.
186+
187+
### [Caveat: Signal handling](@id py-multi-threading-signal-handling)
188+
189+
We recommend setting [`PYTHON_JULIACALL_HANDLE_SIGNALS=yes`](@ref julia-config)
190+
before importing JuliaCall with multiple threads.
191+
192+
This is because Julia intentionally causes segmentation faults as part of the GC
193+
safepoint mechanism. If unhandled, these segfaults will result in termination of the
194+
process. See discussion
195+
[here](https://github.com/JuliaPy/PythonCall.jl/issues/219#issuecomment-1605087024)
196+
for more information.
197+
198+
Note however that this interferes with Python's own signal handling, so for example
199+
Ctrl-C will not raise `KeyboardInterrupt`.
200+
201+
Future versions of JuliaCall may make this the default behaviour when using multiple
202+
threads.

docs/src/pycall.md

-75
This file was deleted.

docs/src/pythoncall-reference.md

+13
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,19 @@ Py(x::MyType) = x.py
218218
@pyconst
219219
```
220220

221+
## Multi-threading
222+
223+
These functions are not exported. They support multi-threading of Python and/or Julia.
224+
See also [`juliacall.AnyValue._jl_call_nogil`](@ref julia-wrappers).
225+
226+
```@docs
227+
PythonCall.GIL.lock
228+
PythonCall.GIL.@lock
229+
PythonCall.GIL.unlock
230+
PythonCall.GIL.@unlock
231+
PythonCall.GC.gc
232+
```
233+
221234
## The Python interpreter
222235

223236
These functions are not exported. They give information about which Python interpreter is

docs/src/pythoncall.md

+40
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,43 @@ end
362362

363363
If your package depends on some Python packages, you must generate a `CondaPkg.toml` file.
364364
See [Installing Python packages](@ref python-deps).
365+
366+
## [Multi-threading](@id jl-multi-threading)
367+
368+
From v0.9.22, PythonCall supports multi-threading in Julia and/or Python, with some
369+
caveats.
370+
371+
Most importantly, you can only call Python code while Python's
372+
[Global Interpreter Lock (GIL)](https://docs.python.org/3/glossary.html#term-global-interpreter-lock)
373+
is locked by the current thread. Ordinarily, the GIL is locked by the main thread in Julia,
374+
so if you want to run Python code on any other thread, you must unlock the GIL from the
375+
main thread and then re-lock it while running any Python code on other threads.
376+
377+
This is made possible by the macros [`PythonCall.GIL.@unlock`](@ref) and
378+
[`PythonCall.GIL.@lock`](@ref) or the functions [`PythonCall.GIL.unlock`](@ref) and
379+
[`PythonCall.GIL.lock`](@ref) with this pattern:
380+
381+
```julia
382+
PythonCall.GIL.@unlock Threads.@threads for i in 1:4
383+
PythonCall.GIL.@lock pyimport("time").sleep(5)
384+
end
385+
```
386+
387+
In the above example, we call `time.sleep(5)` four times in parallel. If Julia was
388+
started with at least four threads (`julia -t4`) then the above code will take about
389+
5 seconds.
390+
391+
Both `@unlock` and `@lock` are important. If the GIL were not unlocked, then a deadlock
392+
would occur when attempting to lock the already-locked GIL from the threads. If the GIL
393+
were not re-locked, then Python would crash when interacting with it.
394+
395+
You can also use [multi-threading from Python](@ref py-multi-threading).
396+
397+
### Caveat: Garbage collection
398+
399+
If Julia's GC collects any Python objects from a thread where the GIL is not currently
400+
locked, then those Python objects will not immediately be deleted. Instead they will be
401+
queued to be deleted in a later GC pass.
402+
403+
If you find you have many Python objects not being deleted, you can call
404+
[`PythonCall.GC.gc()`](@ref) or `GC.gc()` while the GIL is locked to clear the queue.

docs/src/releasenotes.md

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
* `GC.disable()` and `GC.enable()` are now a no-op and deprecated since they are no
88
longer required for thread-safety. These will be removed in v1.
99
* Adds `GC.gc()`.
10+
* Adds module `GIL` with `lock()`, `unlock()`, `@lock` and `@unlock` for handling the
11+
Python Global Interpreter Lock. In combination with the above improvements, these
12+
allow Julia and Python to co-operate on multiple threads.
13+
* Adds method `_jl_call_nogil` to `juliacall.AnyValue` and `juliacall.RawValue` to call
14+
Julia functions with the GIL unlocked.
1015

1116
## 0.9.21 (2024-07-20)
1217
* `Serialization.serialize` can use `dill` instead of `pickle` by setting the env var `JULIA_PYTHONCALL_PICKLE=dill`.

0 commit comments

Comments
 (0)