1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
# sAsync:
# An enhancement to the SQLAlchemy package that provides persistent
# dictionaries, text indexing and searching, and an access broker for
# conveniently managing database access, table setup, and
# transactions. Everything is run in an asynchronous fashion using the Twisted
# framework and its deferred processing capabilities.
#
# Copyright (C) 2006-2007 by Edwin A. Suominen, http://www.eepatents.com
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the file COPYING for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
"""
Misc stuff, including an object for keeping track of deferreds
"""
from twisted.internet import defer
class DeferredTracker(object):
"""
I allow you to track and wait for deferreds without actually having
received a reference to them.
"""
def __init__(self):
self.list = []
def put(self, d):
"""
Put another deferred in the tracker.
"""
if not isinstance(d, defer.Deferred):
raise TypeError("Object '%s' is not a deferred" % d)
self.list.append(d)
def deferToAll(self):
"""
Return a deferred that tracks all active deferreds that aren't yet
being tracked. When the tracked deferreds fire, the returned deferred
fires, too.
"""
if self.list:
d = defer.DeferredList(self.list)
self.list = []
elif hasattr(self, 'd_WFA') and not self.d_WFA.called():
d = defer.Deferred()
self.d_WFA.chainDeferred(d)
else:
d = defer.succeed(None)
return d
def deferToLast(self):
"""
Return a deferred that tracks the deferred that was most recently put
in the tracker. When the tracked deferred fires, the returned deferred
fires, too.
"""
if self.list:
d = defer.Deferred()
self.list.pop().chainDeferred(d)
elif hasattr(self, 'd_WFL') and not self.d_WFL.called():
d = defer.Deferred()
self.d_WFL.chainDeferred(d)
else:
d = defer.succeed(None)
return d
|