/usr/share/pyshared/celery/utils/functional.py is in python-celery 2.4.6-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # -*- coding: utf-8 -*-
"""
celery.utils.functional
~~~~~~~~~~~~~~~~~~~~~~~
Utilities for functions.
:copyright: (c) 2009 - 2011 by Ask Solem.
:license: BSD, see LICENSE for more details.
"""
from __future__ import absolute_import
from __future__ import with_statement
from functools import wraps
from threading import Lock
try:
from collections import Sequence
except ImportError:
# <= Py2.5
Sequence = (list, tuple) # noqa
from celery.datastructures import LRUCache
KEYWORD_MARK = object()
def maybe_list(l):
if isinstance(l, Sequence):
return l
return [l]
def memoize(maxsize=None, Cache=LRUCache):
def _memoize(fun):
mutex = Lock()
cache = Cache(limit=maxsize)
@wraps(fun)
def _M(*args, **kwargs):
key = args + (KEYWORD_MARK, ) + tuple(sorted(kwargs.iteritems()))
try:
with mutex:
value = cache[key]
except KeyError:
value = fun(*args, **kwargs)
_M.misses += 1
with mutex:
cache[key] = value
else:
_M.hits += 1
return value
def clear():
"""Clear the cache and reset cache statistics."""
cache.clear()
_M.hits = _M.misses = 0
_M.hits = _M.misses = 0
_M.clear = clear
_M.original_func = fun
return _M
return _memoize
|