[Twisted-Python] getting all results from DeferredList

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Wed Jun 30 14:42:59 EDT 2010

On 02:41 pm, petshmidt at googlemail.com wrote:
>I'm trying to process results from several Deferreds (d1, d2) with
>DeferredList, merging it into one list and sending back to client.

You might like twisted.internet.defer.gatherResults.
>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?

I don't think I understand this part of the question.
>BTW, are nested runInteraction ok?

Not really.  Twisted APIs are almost all required to be invoked in the 
reactor thread.  Nested runInteraction calls would mean calling 
runInteraction in some other thread, and that's not allowed.

>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:
>                out.extend(r)
>            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)
>        d.addErrback(log.err)
>        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)
>Twisted-Python mailing list
>Twisted-Python at twistedmatrix.com

More information about the Twisted-Python mailing list