datastructures.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. from __future__ import generators
  2. import time
  3. import traceback
  4. from UserList import UserList
  5. from Queue import Queue, Empty as QueueEmpty
  6. from celery.utils.compat import OrderedDict
  7. class AttributeDict(dict):
  8. """Dict subclass with attribute access."""
  9. def __getattr__(self, key):
  10. try:
  11. return self[key]
  12. except KeyError:
  13. raise AttributeError("'%s' object has no attribute '%s'" % (
  14. self.__class__.__name__, key))
  15. def __setattr__(self, key, value):
  16. self[key] = value
  17. class PositionQueue(UserList):
  18. """A positional queue of a specific length, with slots that are either
  19. filled or unfilled. When all of the positions are filled, the queue
  20. is considered :meth:`full`.
  21. :param length: see :attr:`length`.
  22. .. attribute:: length
  23. The number of items required for the queue to be considered full.
  24. """
  25. class UnfilledPosition(object):
  26. """Describes an unfilled slot."""
  27. def __init__(self, position):
  28. # This is not used, but is an argument from xrange
  29. # so why not.
  30. self.position = position
  31. def __init__(self, length):
  32. self.length = length
  33. self.data = map(self.UnfilledPosition, xrange(length))
  34. def full(self):
  35. """Returns ``True`` if all of the slots has been filled."""
  36. return len(self) >= self.length
  37. def __len__(self):
  38. """``len(self)`` -> number of slots filled with real values."""
  39. return len(self.filled)
  40. @property
  41. def filled(self):
  42. """Returns the filled slots as a list."""
  43. return filter(lambda v: not isinstance(v, self.UnfilledPosition),
  44. self.data)
  45. class ExceptionInfo(object):
  46. """Exception wrapping an exception and its traceback.
  47. :param exc_info: The exception tuple info as returned by
  48. :func:`traceback.format_exception`.
  49. .. attribute:: exception
  50. The original exception.
  51. .. attribute:: traceback
  52. A traceback from the point when :attr:`exception` was raised.
  53. """
  54. def __init__(self, exc_info):
  55. type_, exception, tb = exc_info
  56. self.exception = exception
  57. self.traceback = ''.join(traceback.format_exception(*exc_info))
  58. def __str__(self):
  59. return self.traceback
  60. def __repr__(self):
  61. return "<%s.%s: %s>" % (
  62. self.__class__.__module__,
  63. self.__class__.__name__,
  64. str(self.exception))
  65. def consume_queue(queue):
  66. """Iterator yielding all immediately available items in a
  67. :class:`Queue.Queue`.
  68. The iterator stops as soon as the queue raises :exc:`Queue.Empty`.
  69. Example
  70. >>> q = Queue()
  71. >>> map(q.put, range(4))
  72. >>> list(consume_queue(q))
  73. [0, 1, 2, 3]
  74. >>> list(consume_queue(q))
  75. []
  76. """
  77. while 1:
  78. try:
  79. yield queue.get_nowait()
  80. except QueueEmpty:
  81. break
  82. class SharedCounter(object):
  83. """Thread-safe counter.
  84. Please note that the final value is not synchronized, this means
  85. that you should not update the value by using a previous value, the only
  86. reliable operations are increment and decrement.
  87. Example
  88. >>> max_clients = SharedCounter(initial_value=10)
  89. # Thread one
  90. >>> max_clients += 1 # OK (safe)
  91. # Thread two
  92. >>> max_clients -= 3 # OK (safe)
  93. # Main thread
  94. >>> if client >= int(max_clients): # Max clients now at 8
  95. ... wait()
  96. >>> max_client = max_clients + 10 # NOT OK (unsafe)
  97. """
  98. def __init__(self, initial_value):
  99. self._value = initial_value
  100. self._modify_queue = Queue()
  101. def increment(self, n=1):
  102. """Increment value."""
  103. self += n
  104. return int(self)
  105. def decrement(self, n=1):
  106. """Decrement value."""
  107. self -= n
  108. return int(self)
  109. def _update_value(self):
  110. self._value += sum(consume_queue(self._modify_queue))
  111. return self._value
  112. def __iadd__(self, y):
  113. """``self += y``"""
  114. self._modify_queue.put(y * +1)
  115. return self
  116. def __isub__(self, y):
  117. """``self -= y``"""
  118. self._modify_queue.put(y * -1)
  119. return self
  120. def __int__(self):
  121. """``int(self) -> int``"""
  122. return self._update_value()
  123. def __repr__(self):
  124. return "<SharedCounter: int(%s)>" % str(int(self))
  125. class LimitedSet(object):
  126. """Kind-of Set with limitations.
  127. Good for when you need to test for membership (``a in set``),
  128. but the list might become to big, so you want to limit it so it doesn't
  129. consume too much resources.
  130. :keyword maxlen: Maximum number of members before we start
  131. deleting expired members.
  132. :keyword expires: Time in seconds, before a membership expires.
  133. """
  134. def __init__(self, maxlen=None, expires=None):
  135. self.maxlen = maxlen
  136. self.expires = expires
  137. self._data = {}
  138. def add(self, value):
  139. """Add a new member."""
  140. self._expire_item()
  141. self._data[value] = time.time()
  142. def clear(self):
  143. """Remove all members"""
  144. self._data.clear()
  145. def pop_value(self, value):
  146. """Remove membership by finding value."""
  147. self._data.pop(value, None)
  148. def _expire_item(self):
  149. """Hunt down and remove an expired item."""
  150. while 1:
  151. if self.maxlen and len(self) >= self.maxlen:
  152. value, when = self.first
  153. if not self.expires or time.time() > when + self.expires:
  154. try:
  155. self.pop_value(value)
  156. except TypeError: # pragma: no cover
  157. continue
  158. break
  159. def __contains__(self, value):
  160. return value in self._data
  161. def update(self, other):
  162. if isinstance(other, self.__class__):
  163. self._data.update(other._data)
  164. else:
  165. self._data.update(other)
  166. def as_dict(self):
  167. return self._data
  168. def __iter__(self):
  169. return iter(self._data.keys())
  170. def __len__(self):
  171. return len(self._data.keys())
  172. def __repr__(self):
  173. return "LimitedSet([%s])" % (repr(self._data.keys()))
  174. @property
  175. def chronologically(self):
  176. return sorted(self._data.items(), key=lambda (value, when): when)
  177. @property
  178. def first(self):
  179. """Get the oldest member."""
  180. return self.chronologically[0]
  181. class LocalCache(OrderedDict):
  182. """Dictionary with a finite number of keys.
  183. Older items expires first.
  184. """
  185. def __init__(self, limit=None):
  186. super(LocalCache, self).__init__()
  187. self.limit = limit
  188. def __setitem__(self, key, value):
  189. while len(self) >= self.limit:
  190. self.popitem(last=False)
  191. super(LocalCache, self).__setitem__(key, value)
  192. class TokenBucket(object):
  193. """Token Bucket Algorithm.
  194. See http://en.wikipedia.org/wiki/Token_Bucket
  195. Most of this code was stolen from an entry in the ASPN Python Cookbook:
  196. http://code.activestate.com/recipes/511490/
  197. :param fill_rate: see :attr:`fill_rate`.
  198. :keyword capacity: see :attr:`capacity`.
  199. .. attribute:: fill_rate
  200. The rate in tokens/second that the bucket will be refilled.
  201. .. attribute:: capacity
  202. Maximum number of tokens in the bucket. Default is ``1``.
  203. .. attribute:: timestamp
  204. Timestamp of the last time a token was taken out of the bucket.
  205. """
  206. def __init__(self, fill_rate, capacity=1):
  207. self.capacity = float(capacity)
  208. self._tokens = capacity
  209. self.fill_rate = float(fill_rate)
  210. self.timestamp = time.time()
  211. def can_consume(self, tokens=1):
  212. if tokens <= self._get_tokens():
  213. self._tokens -= tokens
  214. return True
  215. return False
  216. def expected_time(self, tokens=1):
  217. """Returns the expected time in seconds when a new token should be
  218. available. *Note: consumes a token from the bucket*"""
  219. _tokens = self._get_tokens()
  220. tokens = max(tokens, _tokens)
  221. return (tokens - _tokens) / self.fill_rate
  222. def _get_tokens(self):
  223. if self._tokens < self.capacity:
  224. now = time.time()
  225. delta = self.fill_rate * (now - self.timestamp)
  226. self._tokens = min(self.capacity, self._tokens + delta)
  227. self.timestamp = now
  228. return self._tokens