[Twisted-Python] Multiple callbacks

Tech Aficionado1743 techaficionado1743 at gmail.com
Wed Jan 25 05:04:09 MST 2012


Hello, can anyone help ?
i can't understand why, in the piece of code below (S1),
the parameter (grid) passed to the callback is the same,
while i have 2 grid, 2 deferred and 2 lambda.
The source S2 is working (2 different grid), but inelegant...

Thank you.


#----------- S1 ------------------------
    def OnSubscribe(self):
        for pool, grid in self.grids.items():
            print 'grid = ',grid
            d = self.frame.dbpoolMiddle.runQuery(allSelect.stmts['Set0'],
(pool, self.frame.ctxName))
            print 'deferred = ', d
            f = lambda _:self.gotDataBin(_, grid)
            print 'lambda =', f
            d.addCallback(f)

    def gotDataBin(self, result, grid):
        print 'callback = ', grid
        grid.ReplaceRowsFromSQL(result)

#----------- Output S1 ----------------

grid =  <cfm.wx.CfmGrid.CfmGrid; proxy of <Swig Object of type 'wxGrid *'
at 0x2d61b48> >
deferred =  <Deferred at 0x2e6bd28>
lambda = <function <lambda> at 0x02DCDCF0>

grid =  <cfm.wx.CfmGrid.CfmGrid; proxy of <Swig Object of type 'wxGrid *'
at 0x2d5e788> >
deferred =  <Deferred at 0x2e6bdc8>
lambda = <function <lambda> at 0x02DCDCB0>

callback =  <cfm.wx.CfmGrid.CfmGrid; proxy of <Swig Object of type 'wxGrid
*' at 0x2d5e788> > # expected to get 1st grid here: 0x2d61b48, not two
times 2nd grid
callback =  <cfm.wx.CfmGrid.CfmGrid; proxy of <Swig Object of type 'wxGrid
*' at 0x2d5e788> >

#----------- S2 ------------------------
    def OnSubscribe(self):
        for pool, grid in self.grids.items():
            d = self.frame.dbpoolMiddle.runQuery(allSelect.stmts['Set0'],
(pool, self.frame.ctxName))

            def toto(grid):
                d.addCallback(lambda _:self.gotDataBin(_, grid))

            toto(grid)

    def gotDataBin(self, result, grid):
        print 'callback = ', grid
        grid.ReplaceRowsFromSQL(result)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20120125/06d7990e/attachment.html>


More information about the Twisted-Python mailing list