[Twisted-Python] AbstractDatagramProtocol implementation question

Olivier Parisy olivier.parisy at free.fr
Thu Dec 3 18:33:56 EST 2009

I am studying the implementation of twisted UDP-related classes, and I 
do not understand the point of the "numPorts" attribute of 

It is clearly designed to avoid multiple calls to startProtocol() from 
doStart(), but I fail in imagining a scenario where this may happen 
since, as the comment states, doStart() should only be called by 
makeConnection(), which has an explicit "assert self.transport == None" 
guard which, indeed, prevents multiple calls.

So why bother to count calls?

Moreover, makeConnection() could be called multiple times on the same 
DatagramProtocol instance if it was "bound" to multiple ports (multiple 
calls to listenUDP), which can be an interesting scenario but is 
prevented by :
- the above assertion,
- the fact that AbstractDatagramProtocol.transport is not a collection, 
so only the last Port would be available anyway.

Is my reasoning wrong or incomplete? If not, couldn't we simplify 
AbstractDatagramProtocol implementation by removing numPorts?

Two other small remarks regarding documentation / understandability :
- The note "This does not actually implement IListeningPort" on udp.Port 
lead me to think that IReactorUDP.listenUDP should not be declared as 
returning an object implementing IListeningPort in the first place 
(confused me!).
- I had to read the code to understand that a "transport" and a "port" 
are exactly the same thing in the UDP implementation. The tutorial and 
the documentation were not clear about this point, so I did not make the 
link between the "self.transport" available in DatagramProtocol and the 
value returned by listenUDP (confused me!).


More information about the Twisted-Python mailing list