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