[Twisted-Python] Multiple callbacks

Laurens Van Houtven _ at lvh.cc
Wed Jan 25 09:03:47 EST 2012


Note that that's just pythons late binding semantics, nothing twistedy
going on here :)
On Jan 25, 2012 2:24 PM, "Аркадий Левин" <poisonoff at gmail.com> wrote:

> Hi, you need add "grid" to lambda arguments,
>
> f = lambda _, grip=grip:self.gotDataBin(_, grid)
>
> On Wed, Jan 25, 2012 at 6:04 PM, Tech Aficionado1743
> <techaficionado1743 at gmail.com> wrote:
> > 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)
> >
> >
> > _______________________________________________
> > Twisted-Python mailing list
> > Twisted-Python at twistedmatrix.com
> > http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
> >
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20120125/3c81af06/attachment.htm 


More information about the Twisted-Python mailing list