[Twisted-Python] problem with combining deferreds together

michal salaban emes at pld-linux.org
Mon Aug 15 12:41:37 EDT 2005


hello all,

i've been playing with twisted adbapi for last three days to figure out if
it's a goot framework for application that i'm going to write soon.

i've stuck on one problem (probably i'm not the first, but couldn't google out
the soultion).

i have an object, which data is spread between two tables - the MainTable with
some data and reference to some row in SubTable with additional things.

what i'm trying to do is to create repository class with method .getByID()
that takes ID from MainTable and returns deferred, which in callback evaluates
to fully instatinated and data-populated object.

i've come up to this:

class ObjectRepository:

    def getById(self, id):
        dMain = self.dbpool.runInteraction(
                fetchOneAndMapToDictionary,
                "SELECT * FROM MainTable WHERE id = '%d'" % (id,)
                )
        dMain.addCallback(self._gotMainData)
        return dMain

    def _gotMainData(self, mappedRow):
        dSub = self.dbpool.runInteraction(
                fetchOneAndMapToDictionary,
                "SELECT * FROM SubTable WHERE id = '%d'" % (mappedRow['SubID'],)
                )
        dSub.addCallback(self._gotSubData)
        return [what to return ????]


how can i return the result that consist of data from both queries?
chaining callbacks is impossible. i cannot have dSub before dMain.callback()
is called, and also cannot do dMain.chainDeferred(dSub) in _gotMainData()
because dMain has already been returned to calling code and contains callbacks
attached there.

i suppose the solution is simple, but im 100% newbie to twisted and
event-driven programming.

thanks in advance :)

-- 
michal salaban -=- emes (at) pld-linux.org -=- jabber: emes at jabber.org




More information about the Twisted-Python mailing list