Is there a pool for ThreadingMixIn and ForkingMixIn for SocketServer?

I tried to create an http proxy using BaseHttpServer which is based on SocketServer which got 2 asynchronous Mixins (ThreadingMixIn and ForkingMixIn)

The problem with these two is that they work on each request (allocate a new thread or develop a new subprocess for each request)

Is there a Mixin that uses the let pool, say, 4 subprocesses and 40 threads each, so the requests are handled by those already created threads?

because it will be a big performance boost, and I think it will save some resources.

+5
source share
2 answers

I started a project that solves this problem.

https://github.com/muayyad-alsadi/python-PooledProcessMixIn

, , TODO ( CTRL + C)

+1

concurrent.futures ( stdlib Python 3.2):

from BaseHTTPServer   import HTTPServer, test
from SimpleHTTPServer import SimpleHTTPRequestHandler
from SocketServer     import ThreadingMixIn

from concurrent.futures import ThreadPoolExecutor # pip install futures

class PoolMixIn(ThreadingMixIn):
    def process_request(self, request, client_address):
        self.pool.submit(self.process_request_thread, request, client_address)

def main():
    class PoolHTTPServer(PoolMixIn, HTTPServer):
        pool = ThreadPoolExecutor(max_workers=40)

    test(HandlerClass=SimpleHTTPRequestHandler, ServerClass=PoolHTTPServer)

if __name__=="__main__":
    main()

, .

server.py, :

$ python -mserver

40 http://your_host:8000/.

HTTPServer .

+5

All Articles