[Twisted-Python] problem with combining deferreds together

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@jabber.org

michal salaban wrote:
Give this a try and see how it works for you... def _gotMainData(self, mappedRow): dSub = self.dbpool.runInteraction( fetchOneAndMapToDictionary, "SELECT * FROM SubTable WHERE id = '%d'" % (mappedRow['SubID'],) ) dSub.addCallback(self._gotSubData, mappedRow) return dSub def _gotSubData(self, subMappedRow, mainMappedRow): # Do work here. When adding callbacks to a deferred, you can add arguments that will get passed to the argument, independant of the result from previous callbacks. Like above, you can take the mappedRow dictionary from the first callback and pass it as an argument to the callback for the subrequest. Also, for the new deferred to be useful, you return it from the callback. When Twisted sees that, it'll pause the callback chain until that deferred fires and processes all of its callbacks. It'll take the result that is produced from that deferred and use it as the result for the next callback in the main deferred. -- Jason Fritcher Software Engineer Core Infrastructure Services & Strategy Earthlink, Inc fritcher@corp.earthlink.net (404) 748-7262, x22262

on Mon, Aug 15, 2005 at 01:13:44PM -0400, Jason Fritcher wrote:
Give this a try and see how it works for you...
works perfectly. thanks! :)
and that's the key. the docs I read didn't mention that (or I didn't notice :) -- michal salaban -=- emes (at) pld-linux.org -=- jabber: emes@jabber.org

michal salaban wrote:
Give this a try and see how it works for you... def _gotMainData(self, mappedRow): dSub = self.dbpool.runInteraction( fetchOneAndMapToDictionary, "SELECT * FROM SubTable WHERE id = '%d'" % (mappedRow['SubID'],) ) dSub.addCallback(self._gotSubData, mappedRow) return dSub def _gotSubData(self, subMappedRow, mainMappedRow): # Do work here. When adding callbacks to a deferred, you can add arguments that will get passed to the argument, independant of the result from previous callbacks. Like above, you can take the mappedRow dictionary from the first callback and pass it as an argument to the callback for the subrequest. Also, for the new deferred to be useful, you return it from the callback. When Twisted sees that, it'll pause the callback chain until that deferred fires and processes all of its callbacks. It'll take the result that is produced from that deferred and use it as the result for the next callback in the main deferred. -- Jason Fritcher Software Engineer Core Infrastructure Services & Strategy Earthlink, Inc fritcher@corp.earthlink.net (404) 748-7262, x22262

on Mon, Aug 15, 2005 at 01:13:44PM -0400, Jason Fritcher wrote:
Give this a try and see how it works for you...
works perfectly. thanks! :)
and that's the key. the docs I read didn't mention that (or I didn't notice :) -- michal salaban -=- emes (at) pld-linux.org -=- jabber: emes@jabber.org
participants (2)
-
Jason Fritcher
-
michal salaban