user762391
user762391

Reputation:

separate thread for every connection?

I'm currently developing a twisted project which establishes a connection to another source (basic.lineReceiver) for each client connecting to the server.

the response chain should look like this:

client connects -> client messages id to server -> server establishes custom source connection -> server continuously produces data with incoming messages from the source -> server continuously broadcasts data to the respective client

source:client - 1:1 relation

my guess is to dispatch every source connection to a new thread and listen for incoming messages. very much like in:

Producing content indefinitely in a separate thread for all connections?

but how would i dispatch each new connection with the reactor in a new thread in twisted after it has been started?

Upvotes: 2

Views: 691

Answers (1)

Jean-Paul Calderone
Jean-Paul Calderone

Reputation: 48335

Threads here are a red herring. What you want is to proxy many (or at least several) connections. You can do this in many ways, but if you're going to do it with Twisted, you may as well skip the ways that involve threads.

Take a look at the existing forwarder in Twisted to get some ideas about how this might be done.

The general idea here is that you can use Twisted to set up your outgoing connection and then receive callbacks with data any time data is read from that outgoing connection. Then you can send that data back over your incoming connection. Twisted handles the I/O for you and calls all your code in a single thread (the "reactor thread"), so there is no need for extra threads.

Upvotes: 1

Related Questions