[Twisted-Python] Adding callback to a Deferred that's already running?

Steve Freitas sflist at ihonk.com
Wed Aug 25 22:34:49 MDT 2004

Hi all,

I've got an XML-RPC app that has to grab some data from a SQL database, then 
depending on the results, make some more SQL calls, make some more decisions, 
then finally return a result. So, I'm hip-deep in Deferreds. So, here's the 
idiom I've come up with, and I'm running into a limitation with it, namely 
that I can't seem to add callbacks to a Deferred after it has started 
running. Here's what I'm doing in abbreviated form, and it's an idiom I'd 
like to stick with, if possible:

class MyXmlRpcClass(xmlrpc.XMLRPC):
    self.db = adbapi.ConnectionPool(blah)

  def xmlrpc_foo(self):
    return FooClass(self.db).step1() # This returns a deferred, see below

class FooClass(General):
    def __init__(self, db):
        self.db = db

    def step1(self):
        self.d = self.db.runQuery(blah)
        return self.d

    def step2(self, query):
 if query == 'yadda':
          return 'This bit works'
    def step3(self, data):
        return "Never gets here!"

If I get to the part where it adds the callback for step3, it ends up giving 
an AlreadyCalled exception in Deferred. So, I expect there's a good way to 
add to a running Deferred, no?



More information about the Twisted-Python mailing list