finalizer.py
4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- coding: UTF-8 -*-
"""
Module that defines a Finalizer class responsible for registering and cleaning
finalizer
Created on Mar 7, 2010
:author: Barthelemy Dagenais
"""
from __future__ import unicode_literals, absolute_import
from threading import RLock
from py4j.compat import items
class ThreadSafeFinalizer(object):
"""A `ThreadSafeFinalizer` is a global class used to register weak
reference finalizers (i.e., a weak reference with a callback).
This class is useful when one wants to register a finalizer of an object
with circular references. The finalizer of an object with circular
references might never be called if the object's finalizer is kept by the
same object.
For example, if object A refers to B and B refers to A, A should not keep a
weak reference to itself.
`ThreadSafeFinalizer` is thread-safe and uses reentrant lock on each
operation."""
finalizers = {}
lock = RLock()
@classmethod
def add_finalizer(cls, id, weak_ref):
"""Registers a finalizer with an id.
:param id: The id of the object referenced by the weak reference.
:param weak_ref: The weak reference to register.
"""
with cls.lock:
cls.finalizers[id] = weak_ref
@classmethod
def remove_finalizer(cls, id):
"""Removes a finalizer associated with this id.
:param id: The id of the object for which the finalizer will be
deleted.
"""
with cls.lock:
cls.finalizers.pop(id, None)
@classmethod
def clear_finalizers(cls, clear_all=False):
"""Removes all registered finalizers.
:param clear_all: If `True`, all finalizers are deleted. Otherwise,
only the finalizers from an empty weak reference are deleted
(i.e., weak references pointing to inexistent objects).
"""
with cls.lock:
if clear_all:
cls.finalizers.clear()
else:
for id, ref in items(cls.finalizers):
if ref() is None:
cls.finalizers.pop(id, None)
class Finalizer(object):
"""A `Finalizer` is a global class used to register weak reference finalizers
(i.e., a weak reference with a callback).
This class is useful when one wants to register a finalizer of an object
with circular references. The finalizer of an object with circular
references might never be called if the object's finalizer is kept by the
same object.
For example, if object A refers to B and B refers to A, A should not keep a
weak reference to itself.
`Finalizer` is not thread-safe and should only be used by single-threaded
programs."""
finalizers = {}
@classmethod
def add_finalizer(cls, id, weak_ref):
"""Registers a finalizer with an id.
:param id: The id of the object referenced by the weak reference.
:param weak_ref: The weak reference to register.
"""
cls.finalizers[id] = weak_ref
@classmethod
def remove_finalizer(cls, id):
"""Removes a finalizer associated with this id.
:param id: The id of the object for which the finalizer will be
deleted.
"""
cls.finalizers.pop(id, None)
@classmethod
def clear_finalizers(cls, clear_all=False):
"""Removes all registered finalizers.
:param clear_all: If `True`, all finalizers are deleted. Otherwise,
only the finalizers from an empty weak reference are deleted (i.e.,
weak references pointing to inexistent objects).
"""
if clear_all:
cls.finalizers.clear()
else:
for id, ref in items(cls.finalizers):
if ref() is None:
cls.finalizers.pop(id, None)
def clear_finalizers(clear_all=False):
"""Removes all registered finalizers in :class:`ThreadSafeFinalizer` and
:class:`Finalizer`.
:param clear_all: If `True`, all finalizers are deleted. Otherwise, only
the finalizers from an empty weak reference are deleted (i.e., weak
references pointing to inexistent objects).
"""
ThreadSafeFinalizer.clear_finalizers(clear_all)
Finalizer.clear_finalizers(clear_all)