Skip to content

Commit

Permalink
Merge pull request DIRACGrid#7670 from DIRACGridBot/cherry-pick-2-2af…
Browse files Browse the repository at this point in the history
…787193-integration

[sweep:integration] Replace __del__ with weakref.finalize in DictCache
  • Loading branch information
fstagni authored Jun 12, 2024
2 parents 90b8855 + f151237 commit 9c0a414
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
49 changes: 24 additions & 25 deletions src/DIRAC/Core/Utilities/DictCache.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import datetime
import threading
import weakref

# DIRAC
from DIRAC.Core.Utilities.LockRing import LockRing
Expand Down Expand Up @@ -63,6 +64,9 @@ def __init__(self, deleteFunction=False, threadLocal=False):
# Function to clean the elements
self.__deleteFunction = deleteFunction

# Called when this object is deleted or the program ends
self.__finalizer = weakref.finalize(self, _purgeAll, None, self.__cache, self.__deleteFunction)

@property
def lock(self):
"""Return the lock.
Expand Down Expand Up @@ -221,31 +225,26 @@ def purgeExpired(self, expiredInSeconds=0):

def purgeAll(self, useLock=True):
"""Purge all entries
CAUTION: useLock parameter should ALWAYS be True except when called from __del__
CAUTION: useLock parameter should ALWAYS be True
:param bool useLock: use lock
"""
if useLock:
self.lock.acquire()
try:
for cKey in list(self.__cache):
if self.__deleteFunction:
self.__deleteFunction(self.__cache[cKey]["value"])
del self.__cache[cKey]
finally:
if useLock:
self.lock.release()

def __del__(self):
"""When the DictCache is deleted, all the entries should be purged.
This is particularly useful when the DictCache manages files
CAUTION: if you carefully read the python doc, you will see all the
caveat of __del__. In particular, no guaranty that it is called...
(https://docs.python.org/2/reference/datamodel.html#object.__del__)
"""
self.purgeAll(useLock=False)
del self.__lock
if self.__threadLocal:
del self.__threadLocalCache
else:
del self.__sharedCache
_purgeAll(self.lock if useLock else None, self.__cache, self.__deleteFunction)


def _purgeAll(lock, cache, deleteFunction):
"""Purge all entries
This is split in to a helper function to be used by the finalizer without
needing to add a reference to the DictCache object itself.
"""
if lock:
lock.acquire()
try:
for cKey in list(cache):
if deleteFunction:
deleteFunction(cache[cKey]["value"])
del cache[cKey]
finally:
if lock:
lock.release()
3 changes: 2 additions & 1 deletion src/DIRAC/FrameworkSystem/Client/ProxyManagerClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def __init__(self):
self.__pilotProxiesCache = DictCache()
self.__filesCache = DictCache(self.__deleteTemporalFile)

def __deleteTemporalFile(self, filename):
@staticmethod
def __deleteTemporalFile(filename):
"""Delete temporal file
:param str filename: path to file
Expand Down

0 comments on commit 9c0a414

Please sign in to comment.