[Twisted-Python] Patterns in Twisted

Itamar Shtull-Trauring lists at itamarst.org
Sun Sep 16 08:10:39 EDT 2001

I've been reading POSA2, and I see Twisted uses a lot of the patterns there. 
You can find more info on each pattern by searching for it in google - most 
of the content in the book is also in publically accessable papers that can 
be found on the web. I'm including here the one sentence description from 

= Reactor =
"* The Reactor  architectural pattern allows event-driven applications to 
demultiplex and dispatch service requests that are delivered to an 
application from one or more clients."

The main event loop in twisted.internt.main is a Reactor - it reacts to 
events from select() and Delayed objects and dispatches them.

= Acceptor-Connector =
"* The Acceptor-Connector design pattern decouples the connection and 
initialization of cooperating peer services in a networked system from the 
processing performed by the peer services after they are connected and 

This is how twisted works as well - once a connection has been accepted, a 
new Protocol object is created that registers itself with the event loop and 
from then on it deals with this connection.

= Active Object =
"* The Active Object design pattern decouples method execution from method 
invocation to enhance concurrency and simplify synchronized access to 
objects that reside in their own threads of control."

It does this by having a separate thread that runs the methods that. Which 
is how internet.main.threadtask works - threads add tasks to it which are 
then executed by the event loop's thread.

= Async / Half-Async =
"* The Half-Sync/Half-Async architectural pattern decouples asynchronous and 
synchronous service processing in concurrent systems, to simplify 
programming without unduly reducing performance. The pattern introduces two 
intercommunicating layers, one for asynchronous and one for synchronous 
service processing."

This is how threading protocols can be created (see the echoserv_threaded.py 
example). Protocol instances in the async layer add received messages to a 
queue that the threads read, and their results are added back to async layer 
using an implicit queuing layer, the threadtask Active Object.

Strategized Locking is what threadable.synchronize does, I think, Wrapper 
Facade is is used in making the transport level abstract, and Component 
Configurator is implicit in twisted's abilities to add/remove/configure 
different components at runtime.

More information about the Twisted-Python mailing list