comparison src/tools/sat_defer.py @ 1549:e987325c14ef

core (tools): “DelayedDeferred” implementation, which is a Deferred launched after a delay
author Goffi <goffi@goffi.org>
date Mon, 02 Nov 2015 22:02:41 +0100
parents 227856b13d7a
children d17772b0fe22
comparison
equal deleted inserted replaced
1548:61c03265e133 1549:e987325c14ef
21 21
22 from sat.core.log import getLogger 22 from sat.core.log import getLogger
23 log = getLogger(__name__) 23 log = getLogger(__name__)
24 from sat.core import exceptions 24 from sat.core import exceptions
25 from twisted.internet import defer 25 from twisted.internet import defer
26 from twisted.internet import error as internet_error
27 from twisted.internet import reactor
26 from twisted.python import failure 28 from twisted.python import failure
27 from sat.core.constants import Const as C 29 from sat.core.constants import Const as C
28 from sat.memory import memory 30 from sat.memory import memory
29 31
30 KEY_DEFERREDS = 'deferreds' 32 KEY_DEFERREDS = 'deferreds'
31 KEY_NEXT = 'next_defer' 33 KEY_NEXT = 'next_defer'
34
35
36 class DelayedDeferred(object):
37 """A Deferred-like which is launched after a delay"""
38
39 def __init__(self, delay, result):
40 """
41 @param delay(float): delay before launching the callback, in seconds
42 @param result: result used with the callback
43 """
44 self._deferred = defer.Deferred()
45 self._timer = reactor.callLater(delay, self._deferred.callback, result)
46
47 def cancel(self):
48 try:
49 self._timer.cancel()
50 except internet_error.AlreadyCalled:
51 pass
52 self._deferred.cancel()
53
54 def addCallbacks(self, *args, **kwargs):
55 self._deferred.addCallbacks(*args,**kwargs)
56
57 def addCallback(self, *args, **kwargs):
58 self._deferred.addCallback(*args,**kwargs)
59
60 def addErrback(self, *args, **kwargs):
61 self._deferred.addErrback(*args,**kwargs)
62
63 def addBoth(self, *args, **kwargs):
64 self._deferred.addBoth(*args,**kwargs)
65
66 def chainDeferred(self, *args, **kwargs):
67 self._deferred.chainDeferred(*args,**kwargs)
68
69 def pause(self):
70 self._deferred.pause()
71
72 def unpause(self):
73 self._deferred.unpause()
32 74
33 75
34 class RTDeferredSessions(memory.Sessions): 76 class RTDeferredSessions(memory.Sessions):
35 """Real Time Deferred Sessions""" 77 """Real Time Deferred Sessions"""
36 78