7

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

the problem with those two that they work on each request (allocate a new thread or fork a new subprocess for each request)

is there a Mixin that utilize a pool of let's say 4 subprocesses and 40 threads in each so requests get handled by those already created threads ?

because this would be a big performance gain and I guess it would save some resources.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Muayyad Alsadi
  • 1,506
  • 15
  • 23

2 Answers2

13

You could use a pool from concurrent.futures (in stdlib since 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()

As you can see the implementation for a threading case is rather trivial.

If you save it to server.py then you could run it as:

$ python -mserver

This command uses upto 40 threads to serve requests on http://your_host:8000/.

The main use case of HTTPServer is for testing purposes.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • this is similar to using the multiprocessing pool, and this is not the optimal solutions, for example it involves queues and pickles ..etc. please see [my solution](https://github.com/muayyad-alsadi/python-PooledProcessMixIn/wiki) features. – Muayyad Alsadi Aug 28 '12 at 09:03
  • @muayyad: 1. this as well as stdlib classes are for development/testing purposes 2. I can expect that the above works due to its simplicity. I can't say the same about your solution 3. do you have any benchmark? – jfs Aug 28 '12 at 09:36
  • 1
    @j-f-sebastian: I've used siege to benchmark, it doubles the performance (mine trans Rate: 1530, other mixins about 800) – Muayyad Alsadi Oct 14 '12 at 12:28
3

I've started a project that solves this issue

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

maybe you want to join me finish the TODOs (clean up after CTRL+C)

Muayyad Alsadi
  • 1,506
  • 15
  • 23