MCPcopy
hub / github.com/celery/celery / PromiseProxy

Class PromiseProxy

celery/local.py:281–349  ·  view source on GitHub ↗

Proxy that evaluates object once. :class:`Proxy` will evaluate the object each time, while the promise will only evaluate it once.

Source from the content-addressed store, hash-verified

279
280
281class PromiseProxy(Proxy):
282 """Proxy that evaluates object once.
283
284 :class:`Proxy` will evaluate the object each time, while the
285 promise will only evaluate it once.
286 """
287
288 __slots__ = ('__pending__', '__weakref__')
289
290 def _get_current_object(self):
291 try:
292 return object.__getattribute__(self, '__thing')
293 except AttributeError:
294 return self.__evaluate__()
295
296 def __then__(self, fun, *args, **kwargs):
297 if self.__evaluated__():
298 return fun(*args, **kwargs)
299 from collections import deque
300 try:
301 pending = object.__getattribute__(self, '__pending__')
302 except AttributeError:
303 pending = None
304 if pending is None:
305 pending = deque()
306 object.__setattr__(self, '__pending__', pending)
307 pending.append((fun, args, kwargs))
308
309 def __evaluated__(self):
310 try:
311 object.__getattribute__(self, '__thing')
312 except AttributeError:
313 return False
314 return True
315
316 def __maybe_evaluate__(self):
317 return self._get_current_object()
318
319 def __evaluate__(self,
320 _clean=('_Proxy__local',
321 '_Proxy__args',
322 '_Proxy__kwargs')):
323 try:
324 thing = Proxy._get_current_object(self)
325 except Exception:
326 raise
327 else:
328 object.__setattr__(self, '__thing', thing)
329 for attr in _clean:
330 try:
331 object.__delattr__(self, attr)
332 except AttributeError: # pragma: no cover
333 # May mask errors so ignore
334 pass
335 try:
336 pending = object.__getattribute__(self, '__pending__')
337 except AttributeError:
338 pass

Callers 4

test_callbacksMethod · 0.90
test_maybe_evaluateMethod · 0.90
_create_task_clsMethod · 0.90

Calls

no outgoing calls

Tested by 3

test_callbacksMethod · 0.72
test_maybe_evaluateMethod · 0.72