summaryrefslogtreecommitdiff
path: root/psutil/_common.py
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2018-12-13 15:54:37 +0100
committerGiampaolo Rodola <g.rodola@gmail.com>2018-12-13 15:54:37 +0100
commit2cdf81db322822ba8fb23ed67523aacb6539da95 (patch)
treeee946d6f3200eafd2dafdee78e68c899426ebf8e /psutil/_common.py
parent8351fa4ff642d997cd478a7d216b661e62a5f696 (diff)
downloadpsutil-2cdf81db322822ba8fb23ed67523aacb6539da95.tar.gz
#1373: different approach to oneshot() cache (pass Process instances around - which is faster)
Diffstat (limited to 'psutil/_common.py')
-rw-r--r--psutil/_common.py25
1 files changed, 13 insertions, 12 deletions
diff --git a/psutil/_common.py b/psutil/_common.py
index bee95792..f498bb90 100644
--- a/psutil/_common.py
+++ b/psutil/_common.py
@@ -327,7 +327,7 @@ def memoize_when_activated(fun):
1
>>>
>>> # activated
- >>> foo.cache_activate()
+ >>> foo.cache_activate(self)
>>> foo()
1
>>> foo()
@@ -336,26 +336,27 @@ def memoize_when_activated(fun):
"""
@functools.wraps(fun)
def wrapper(self):
- if not wrapper.cache_activated:
+ if not hasattr(self, "_cache"):
return fun(self)
else:
try:
- ret = cache[fun]
+ ret = self._cache[fun]
except KeyError:
- ret = cache[fun] = fun(self)
+ ret = self._cache[fun] = fun(self)
return ret
- def cache_activate():
- """Activate cache."""
- wrapper.cache_activated = True
+ def cache_activate(proc):
+ """Activate cache. Expects a Process instance. Cache will be
+ stored as a "_cache" instance attribute."""
+ proc._cache = {}
- def cache_deactivate():
+ def cache_deactivate(proc):
"""Deactivate and clear cache."""
- wrapper.cache_activated = False
- cache.clear()
+ try:
+ del proc._cache
+ except AttributeError:
+ pass
- cache = {}
- wrapper.cache_activated = False
wrapper.cache_activate = cache_activate
wrapper.cache_deactivate = cache_deactivate
return wrapper