[Twisted-Python] Twisted in a multicore environment

Ray Cote rgacote at appropriatesolutions.com
Thu Jul 12 16:44:32 EDT 2012

Hi Augusto:

You don't mention the protocol you're implementing in Twisted, but if is is HTTP...

The way we address this problem with an HTTP protocol is to put 
multiple copies of Twisted behind nginx. 

On two and four core machines, we set the number of nginx worker processes equal 
to the number of CPU cores (as recommend by nginx docs). 
We then install one Twisted server instance per core.
We then have nginx round-robin proxy into the Twisted instances.

On a larger system (such as eight cores) we'd set up four nginx worker processes
and four Twisted services; or perhaps two nginx worker processes and six Twisted
services. Particularly when we're running HTTPS and the 
nginx Web Application Firewall which can consume a fair bit of CPU. 

Final balancing between nginx and Twisted core is obviously dependent on where the
heaviest computation appears. 

We find this balancing act works well and, since we know the number of 
cores on a box when we're deploying, we're rarely interested in the ability to 
add/remove Twisted instances on the fly. 


----- Original Message -----
> From: "Augusto Mecking Caringi" <augustocaringi at gmail.com>
> To: "Twisted general discussion" <twisted-python at twistedmatrix.com>
> Sent: Tuesday, July 10, 2012 11:14:27 AM
> Subject: [Twisted-Python] Twisted in a multicore environment
> Hi,
>     I'm researching the best way to implement/use a Twisted-based
> server in a multicore environment...
>     There is the Ampoule project, that I realize is considered the
> best way to do that. Right?
>     I'm also reading about the internals of Nginx HTTP server. This
> server utilizes the same reactor pattern of Twisted (epoll based)...
>     "What resulted is a modular, event-driven, asynchronous,
> single-threaded, non-blocking architecture which became the
> foundation
> of nginx code." http://www.aosabook.org/en/nginx.html
>     But to maximize the use of processors in a multicore environment,
> Nginx do this:
>     "nginx doesn't spawn a process or thread for every connection.
> Instead, worker processes accept new requests from a shared "listen"
> socket and execute a highly efficient run-loop inside each worker to
> process thousands of connections per worker"
>     My question: There is something similar in Twisted? Or do you
> think that is easy to implement something like that?
>     Thanks!
> --
> Augusto Mecking Caringi
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

Ray Cote, President
Appropriate Solutions, Inc.
We Build Software

More information about the Twisted-Python mailing list