<div dir="ltr">Thanks Gavin<div><br></div><div>Good to know how things are done at your end. thanks!</div><div>However, why is Twisted not internally using SO_REUSEPORT ? Is there any plan to integrate it inside twisted so that application need not worry about it ? <br></div><div><br></div><div>The problem with code mentioned here:  <a href="http://stackoverflow.com/questions/10077745/twistedweb-on-multicore-multiprocessor">http://stackoverflow.com/questions/10077745/twistedweb-on-multicore-multiprocessor</a></div><div>is that the requests are not equally distributed across all processes. This imbalance leads to underutilization of CPU cores. This issue is also discussed on <a href="https://lwn.net/Articles/542629/">https://lwn.net/Articles/542629/</a></div><div>and that this is not an issue with SO_REUSEPORT</div><div><br></div><div>Does it mean that Twisted is using single listening socket and all processes accept() on that socket ?</div><div><br></div><div>thanks</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jun 10, 2015 at 2:23 AM, Gavin Panella <span dir="ltr"><<a href="mailto:gavin@gromper.net" target="_blank">gavin@gromper.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On 10 June 2015 at 03:01, Sagar Dixit <<a href="mailto:sagar.dixit@gmail.com">sagar.dixit@gmail.com</a>> wrote:<br>
> Hi,<br>
><br>
> I am exploring Twisted Web for my RESTful application. My application<br>
> is stateless and involves storing and retrieving objects based on<br>
> Object-ID. This application will run on beefy (multicore, lots of<br>
> memory) machine. However, not all APIs that the application issues to<br>
> underlying storage are async and hence I cannot fully utilize<br>
> Deferreds<br>
> Which means, there will some blocking calls and hence my primary<br>
> interest is to use Twisted Web in multiprocessing mode<br>
><br>
> I came across<br>
> <a href="http://stackoverflow.com/questions/10077745/twistedweb-on-multicore-multiprocessor" target="_blank">http://stackoverflow.com/questions/10077745/twistedweb-on-multicore-multiprocessor</a><br>
><br>
> However, I am not sure if it is the "correct" way of doing things.<br>
><br>
> Hence I had some questions around it:<br>
><br>
> 1. Is there an interface (similar to defertoThread) which allows me to<br>
> execute a blocking call in a separate process ?<br>
><br>
> 2. Does reactor synchronize access of all processes to the shared<br>
> listen socket ?<br>
><br>
> 3. Is there a sample code I can refer to where the application is<br>
> spawning subprocesses to handle HTTP requests ?<br>
<br>
</div></div>We've used SO_REUSEPORT (which is briefly mentioned on the SO page you<br>
linked to) in order to run multiple processes. On a recentish Linux the<br>
following approach has worked well for us:<br>
<br>
    # Make a socket with SO_REUSEPORT set so that we can run multiple web<br>
    # applications. This is easier to do from outside of Twisted as there's<br>
    # not yet official support for setting socket options.<br>
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<br>
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)<br>
    # The following might not work on older kernels, or SO_REUSEPORT<br>
    # might not be available if Python was compiled with older headers.<br>
    # In the former case you're out of luck, but you can define the<br>
    # header yourself if it's missing from Python's socket module.<br>
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)<br>
<br>
    # Listen on all interfaces on port 1234.<br>
    s.bind(('0.0.0.0', 1234))<br>
<br>
    # Use a backlog of 50, which seems to be fairly common.<br>
    s.listen(50)<br>
<br>
    # Adopt this socket into something more Twisty.<br>
    endpoint = AdoptedStreamServerEndpoint(reactor, s.fileno(), s.family)<br>
<br>
    # Prevent garbage collection. This is something we discovered we<br>
    # needed, and is perhaps something that AdoptedStreamServerEndpoint<br>
    # ought to do itself.<br>
    endpoint.socket = s<br>
<br>
    # Create a service with the endpoint.<br>
    service = OurService(site_endpoint)<br>
<br>
(Derived from src/maasserver/eventloop.py in MAAS.)<br>
<br>
We then use Upstart (Ubuntu 14.10 and before) or systemd (Ubuntu 15.04)<br>
to run multiple processes. For us this mean 4, but you can have as many<br>
or as few as you need, for example 1 process per CPU core. I like this<br>
approach because we can use the system's facilities for process<br>
supervision instead of cobbling together our own.<br>
<br>
One caveat is that the processes are running concurrently. If you're<br>
modifiying shared resources you'll want to consider explicit locking<br>
where before you might have been safe with Twisted' single-threadedness.<br>
<br>
Gavin.<br>
<br>
_______________________________________________<br>
Twisted-web mailing list<br>
<a href="mailto:Twisted-web@twistedmatrix.com">Twisted-web@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">ssdixit<br><br></div>
</div>