Search code examples
pythonmultithreadingtwistedpython-multithreading

python twisted multithreaded server


As I was reading twisted docs on multithreading, I had the impression, that twisted works the best in a single thread, doing all the job asynchronously. But I've got a strong feeling that I don't understand the relation between multithreading, asynchronicity and performance (speed).

Imagine we've got a computer with many CPU cores. We want to deploy a twisted server there, which would manage high traffic. We can make use of all the cores to make the server respond as fast as possible. What should we do? Shall I use single or multi -threaded pattern? Does number of cores affect twisted efficiency (on C level or on python level)?

Just to mention, I'm not considering any cache, database -related topics (which could speed up any app). I'm considering only hardware-related topics along with threads, processes, etc.


Solution

  • Spawn multiple processes, using Twisted's built-in process-spawning API.

    You can use the reactor socket-transferring methods to move connections and listening ports between processes in your server if your server is going to run on UNIX (although these interfaces may one day be ported to Windows as well).

    Spawning multiple threads in Python (regardless of whether you are using Twisted or a system like it) will not allow you to make use of multiple cores, because of the global interpreter lock.