diff 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
line wrap: on
line diff
--- a/src/tools/sat_defer.py	Mon Nov 02 22:02:41 2015 +0100
+++ b/src/tools/sat_defer.py	Mon Nov 02 22:02:41 2015 +0100
@@ -23,6 +23,8 @@
 log = getLogger(__name__)
 from sat.core import exceptions
 from twisted.internet import defer
+from twisted.internet import error as internet_error
+from twisted.internet import reactor
 from twisted.python import failure
 from sat.core.constants import Const as C
 from sat.memory import memory
@@ -31,6 +33,46 @@
 KEY_NEXT = 'next_defer'
 
 
+class DelayedDeferred(object):
+    """A Deferred-like which is launched after a delay"""
+
+    def __init__(self, delay, result):
+        """
+        @param delay(float): delay before launching the callback, in seconds
+        @param result: result used with the callback
+        """
+        self._deferred = defer.Deferred()
+        self._timer = reactor.callLater(delay, self._deferred.callback, result)
+
+    def cancel(self):
+        try:
+            self._timer.cancel()
+        except internet_error.AlreadyCalled:
+            pass
+        self._deferred.cancel()
+
+    def addCallbacks(self, *args, **kwargs):
+        self._deferred.addCallbacks(*args,**kwargs)
+
+    def addCallback(self, *args, **kwargs):
+        self._deferred.addCallback(*args,**kwargs)
+
+    def addErrback(self, *args, **kwargs):
+        self._deferred.addErrback(*args,**kwargs)
+
+    def addBoth(self, *args, **kwargs):
+        self._deferred.addBoth(*args,**kwargs)
+
+    def chainDeferred(self, *args, **kwargs):
+        self._deferred.chainDeferred(*args,**kwargs)
+
+    def pause(self):
+        self._deferred.pause()
+
+    def unpause(self):
+        self._deferred.unpause()
+
+
 class RTDeferredSessions(memory.Sessions):
     """Real Time Deferred Sessions"""