References:
Reactor Pattern
在Twisted中3个关键的概念:
-
Transports
The Transport abstraction is defined by
ITransport
in the main Twistedinterfaces
module. A Twisted Transport represents a single connection that can send and/or receive bytes. For our poetry clients, the Transports are abstractingTCP connections like the ones we have been making ourselves in earlier versions. But Twisted also supports I/O overUNIX Pipes and UDP sockets among other things. The Transport abstraction represents any such connection and handles the details of asynchronous I/O for whatever sort of connection it represents.
-
Protocols
Twisted Protocols are defined by
IProtocol
in the sameinterfaces
module. As you might expect, Protocol objects implement protocols. That is to say, a particular implementation of a Twisted Protocol should implement one specific networking protocol, like FTP or IMAP or some nameless protocol we invent for our own purposes. Our poetry protocol, such as it is, simply sends all the bytes of the poem as soon as a connection is established, while the close of the connection signifies the end of the poem. -
Protocol Factories
Protocol Factories are an example of the Factory design pattern and they work in a straightforward way. The
buildProtocol
method is supposed to return a new Protocol instance each time it is called. This is the method that Twisted uses to make a new Protocol for each new connection.