[Twisted-Python] Question about writing a server that accepts multiple clients with independent processing
mailing_lists at evotex.ch
Thu Feb 21 05:07:24 EST 2008
Stephen Thorne wrote:
> 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 connection.
> 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
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
Thank you Stephan, ok, but is an instance of a protocol created for
every connection? I tried to add data received by the protocol to a
list, and if I get multiple connections that list is filled with all the
connection's data, like if only one instance existed.
More information about the Twisted-Python