[Twisted-Python] getting all results from DeferredList

Pet petshmidt at googlemail.com
Wed Jun 30 10:41:38 EDT 2010


I'm trying to process results from several Deferreds (d1, d2) with
DeferredList, merging it into one list and sending back to client.
While merging works, the client has no results. What is the way to do
tasks (db queries in my case) in parallel and then merge results?

BTW, are nested runInteraction ok?

Thanks for help!

class Test:
    def __init__(self, cursor):
        self.cursor = cursor

    def test(db, params):
        if params.get('query'):
            params['q'] = self.someFunc3(db, params)

        def processResults(results, out):
            #merge results into one
            for _, r in results:
            return out

        out = []
        d1 = self.cursor.runInteraction(self.someFunc, params)
        d2 = self.cursor.runInteraction(self.someFunc2, params)
        d = DeferredList([d1,d2])
        d.addCallback(processResults, out)
        return d

class XMLRPCProtokoll(xmlrpc.XMLRPC):
    def __init__(self):
        self.db = adbapi.ConnectionPool()
        xmlrpc.XMLRPC.__init__(self, True)

    def xmlrpc_test(self, param):
        t = Test(self.db)
        return self.db.runInteraction(t.test, param)

More information about the Twisted-Python mailing list