[Twisted-Python] Using DeferredList, on the remote method get parameter of None type?
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? Thanks all! ------------------------------------------------------------------------------------------------------------- client end def getTrafficDate(self): if self.interfacenio_dict is not None and self.k: nodelist = self.interfacenio_dict.keys() trafficDataList=[]#DeferredList for node in nodelist: interfaces=self.interfacenio_dict[node]['interfaceinfo'].keys() if len(interfaces): 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)) d2.addCallback(self.setTrafficSpeed_Tap, node, interface) trafficDataList.append(d2) else: d= globals.perspective.callRemote('getServerRoot') d2=d.addCallback(lambda object: object.callRemote("getNIOTrafficInfo", node, nio)) d2.addCallback(self.setTrafficSpeed_NotTap, node, interface) trafficDataList.append(d2) trafficDataDeferredList=defer.DeferredList(trafficDataList, consumeErrors=True) trafficDataDeferredList.addCallback(self.getTrafficDataHandle) server end def remote_getNIOTrafficInfo(self,node,nio): print nio if nio.name==nioName: pass Bug info is: File "/home/gddw/Documents/gddw-server-branche/src/Server.py", line 154, in remote_getNIOTrafficInfo if nio.name==nioName: exceptions.AttributeError: 'NoneType' object has no attribute 'name'
On 02:35 pm, yangyouxiu@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
Thank you very much. I got it, I changed it to use a defer object, not deferredlist From: exarkun Date: 2014-03-05 23:00 To: Twisted general discussion Subject: Re: [Twisted-Python] Using DeferredList, on the remote method get parameter of None type? On 02:35 pm, yangyouxiu@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 _______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (2)
-
exarkun@twistedmatrix.com -
yangyouxiu