3
3
4
4
Garbage collection of Python objects.
5
5
6
- See `disable` and `enable` .
6
+ See [`enable`](@ref) and [`gc`](@ref) .
7
7
"""
8
8
module GC
9
9
@@ -13,32 +13,39 @@ const ENABLED = Ref(true)
13
13
const QUEUE = C. PyPtr[]
14
14
15
15
"""
16
- PythonCall.GC.disable( )
16
+ PythonCall.GC.enable(on::Bool )
17
17
18
- Disable the PythonCall garbage collector .
18
+ Control whether garbage collection of Python objects is turned on or off .
19
19
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.
22
25
23
26
Like most PythonCall functions, you must only call this from the main thread.
24
27
"""
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
28
39
end
29
40
30
41
"""
31
- PythonCall.GC.enable()
32
-
33
- Re-enable the PythonCall garbage collector.
42
+ PythonCall.GC.gc()
34
43
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.
37
45
38
46
Like most PythonCall functions, you must only call this from the main thread.
39
47
"""
40
- function enable ()
41
- ENABLED[] = true
48
+ function gc ()
42
49
if ! isempty (QUEUE)
43
50
C. with_gil (false ) do
44
51
for ptr in QUEUE
@@ -47,9 +54,8 @@ function enable()
47
54
end
48
55
end
49
56
end
57
+ empty! (QUEUE)
50
58
end
51
- empty! (QUEUE)
52
- return
53
59
end
54
60
55
61
function enqueue (ptr:: C.PyPtr )
0 commit comments