[Twisted-Python] Using DeferredList, on the remote method get parameter of None type?

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Wed Mar 5 08:00:47 MST 2014


On 02:35 pm, yangyouxiu at gmail.com wrote:
>
>When developing server-client software, on the client i call the method 
>getTrafficDate, but on the server end, i got nio is 
>None(d2=d.addCallback(lambda object: 
>object.callRemote("getNIOTrafficInfo", node, nio)))
>,so, what's the key point?
>
>[snip]
>        for interface in interfaces:
>            nio= 
>self.interfacenio_dict[node]['interfaceinfo'][interface]
>            if nio is not None:
>                if 'nio_tap' in nio or 'NIO_NF_tap' in nio:
>                    d= globals.perspective.callRemote('getServerRoot')
>                    d2=d.addCallback(
>                        lambda object:
>                            object.callRemote("getNIOTrafficInfo", node, 
>nio))
>[snip]

The value of `nio` varies for each iteration of the loop.  The value of 
`nio` isn't passed to the getNIOTrafficInfo remote method until the `d` 
Deferred fires - this will almost certainly be only after the loop 
finishes (since it depends on network traffic which will only happen 
after this method returns).

So all of the remote calls will be made with whatever value `nio` has on 
the last iteration of the loop.  This is just how scopes work in Python. 
Consider this simplified example:
>>>def foo():
...     functions = []
...     for i in range(3):
...         functions.append(lambda: i)
...     for f in functions:
...         print f()
...
>>>foo()
2
2
2
>>>

Jean-Paul




More information about the Twisted-Python mailing list