Skip to content

Commit 63a3e60

Browse files
authored
make PythonCall.GC more like Base.GC (#413)
Co-authored-by: Christopher Doris <github.com/cjdoris>
1 parent 3ac5f4d commit 63a3e60

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

docs/src/faq.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ No.
66

77
Some rules if you are writing multithreaded code:
88
- Only call Python functions from the first thread.
9-
- You probably also need to call `PythonCall.GC.disable()` on the main thread before any
10-
threaded block of code. Remember to call `PythonCall.GC.enable()` again afterwards.
9+
- You probably also need to call `on=PythonCall.GC.enable(false)` on the main thread before any
10+
threaded block of code. Remember to call `PythonCall.GC.enable(on)` again afterwards.
1111
(This is because Julia finalizers can be called from any thread.)
1212
- Julia intentionally causes segmentation faults as part of the GC safepoint mechanism.
1313
If unhandled, these segfaults will result in termination of the process. To enable signal handling,

docs/src/releasenotes.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Release Notes
22

33
## Unreleased (v1)
4+
* `PythonCall.GC` is now more like `Base.GC`: `enable(true)` replaces `enable()`, `enable(false)` replaces `disable()`, and `gc()` is added.
45

56
## Unreleased
67
* `Py` is now treated as a scalar when broadcasting.

src/GC/GC.jl

+23-17
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
Garbage collection of Python objects.
55
6-
See `disable` and `enable`.
6+
See [`enable`](@ref) and [`gc`](@ref).
77
"""
88
module GC
99

@@ -13,32 +13,39 @@ const ENABLED = Ref(true)
1313
const QUEUE = C.PyPtr[]
1414

1515
"""
16-
PythonCall.GC.disable()
16+
PythonCall.GC.enable(on::Bool)
1717
18-
Disable the PythonCall garbage collector.
18+
Control whether garbage collection of Python objects is turned on or off.
1919
20-
This means that whenever a Python object owned by Julia is finalized, it is not immediately
21-
freed but is instead added to a queue of objects to free later when `enable()` is called.
20+
Return the previous GC state.
21+
22+
Disabling the GC means that whenever a Python object owned by Julia is finalized, it is not
23+
immediately freed but is instead added to a queue of objects to free later when GC is
24+
re-enabled.
2225
2326
Like most PythonCall functions, you must only call this from the main thread.
2427
"""
25-
function disable()
26-
ENABLED[] = false
27-
return
28+
function enable(on::Bool)
29+
was_on = ENABLED[]
30+
if on
31+
ENABLED[] = true
32+
if !was_on
33+
gc()
34+
end
35+
else
36+
ENABLED[] = false
37+
end
38+
return ans
2839
end
2940

3041
"""
31-
PythonCall.GC.enable()
32-
33-
Re-enable the PythonCall garbage collector.
42+
PythonCall.GC.gc()
3443
35-
This frees any Python objects which were finalized while the GC was disabled, and allows
36-
objects finalized in the future to be freed immediately.
44+
Perform garbage collection of Python objects.
3745
3846
Like most PythonCall functions, you must only call this from the main thread.
3947
"""
40-
function enable()
41-
ENABLED[] = true
48+
function gc()
4249
if !isempty(QUEUE)
4350
C.with_gil(false) do
4451
for ptr in QUEUE
@@ -47,9 +54,8 @@ function enable()
4754
end
4855
end
4956
end
57+
empty!(QUEUE)
5058
end
51-
empty!(QUEUE)
52-
return
5359
end
5460

5561
function enqueue(ptr::C.PyPtr)

0 commit comments

Comments
 (0)