This is much like the twisted deferred, but is simplified down for use without twisted.
- class Deferred(object):
- def __init__(self):
- self._callbacks = []
-
- def add_callback(self, callback):
- if hasattr(self, 'result'):
- callback(self.result)
- else:
- self._callbacks.append(callback)
-
- def callback(self, result):
- assert not hasattr(self, 'result')
- for cb in self._callbacks:
- cb(result)
- self.result = result
-
-
- def defer(result):
- d = Deferred()
- d.result = result
- return d
class Deferred(object):
def __init__(self):
self._callbacks = []
def add_callback(self, callback):
if hasattr(self, 'result'):
callback(self.result)
else:
self._callbacks.append(callback)
def callback(self, result):
assert not hasattr(self, 'result')
for cb in self._callbacks:
cb(result)
self.result = result
def defer(result):
d = Deferred()
d.result = result
return d
No comments:
Post a Comment