[Twisted-Python] Question about writing a server that accepts multiple clients with independent processing

Stephen Thorne stephen at thorne.id.au
Thu Feb 21 04:53:47 EST 2008

On 21/02/2008, at 7:31 PM, Gabriel Rossetti wrote:
> I have done this so far using Twisted :
> 1) created a protocol (the one that would be executed in a separate  
> thread in the example above)
> 2) created a factory and listen for incoming connections.
> This works great if I have one connection, but I need multiple  
> connections being processed in parallel (simulated parallelism I  
> guess)

Twisted handles the multiple connections by being event based. When a  
networking event occurs, you can receive notifications of those events.

For instance:

A new connection is established -> Factory is told, and has the  
ability to create and associate a protocol instance with that  
Data comes over a connection -> The Protocol object is told via the  
dataReceived method.
A connection is closed -> The Protocol is told via there  
connectionLost method.

Each time these methods are called, you are expected to return from  
them, not do things in while loops. To get more data, you just return  
control to the event loop (aka reactor.run()) and wait for futher  

In order to send data, you send it to protocol.transport.write 
(allmydata), and the event loop takes the responsibility of sending  
that data to the other end of the connection.

I hope that helps you with the concepts involved here. I suggest  
playing around with the Echo server example if you want a readable  
example you can poke and prod.

Stephen Thorne

More information about the Twisted-Python mailing list