py4j.finalizer
— Py4J Finalizer API¶The py4j.finalizer
module contains global classes that enables the registration of finalizers, i.e., weak
reference callbacks. This module is used by Py4J to register a finalizer for each JavaObject instance: once there is no
more reference to a JavaObject instance on the Python side, the finalizer sends a message to the JVM to remove the
reference from the Gateway to prevent memory leak.
The py4j.finalizer
module is necessary because JavaObject instances have circular references with JavaMethods and
hence, they cannot keep their own finalizer.
py4j.finalizer.
ThreadSafeFinalizer
¶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.
add_finalizer
(id, weak_ref)¶Registers a finalizer with an id.
Parameters: |
|
---|
clear_finalizers
(clear_all=False)¶Removes all registered finalizers.
Parameters: | 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). |
---|
finalizers
= {}¶lock
= <_RLock owner=None count=0>¶remove_finalizer
(id)¶Removes a finalizer associated with this id.
Parameters: | id – The id of the object for which the finalizer will be deleted. |
---|
py4j.finalizer.
Finalizer
¶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.
add_finalizer
(id, weak_ref)¶Registers a finalizer with an id.
Parameters: |
|
---|
clear_finalizers
(clear_all=False)¶Removes all registered finalizers.
Parameters: | 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). |
---|
finalizers
= {}¶remove_finalizer
(id)¶Removes a finalizer associated with this id.
Parameters: | id – The id of the object for which the finalizer will be deleted. |
---|
py4j.finalizer.
clear_finalizers
(clear_all=False)¶Removes all registered finalizers in ThreadSafeFinalizer
and
Finalizer
.
Parameters: | 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). |
---|