The Python socketserver module provides a shortcut for developing socket server. However, its built-in asynchronous handler threadingmixin generates a new thread each time it processes the request, which may cause some problems when the load is heavy. A tcpserver using the thread pool is required.
The specific code is as follows:
From threading import thread <br/> from socketserver import threadingmixin, tcpserver, baserequesthandler <br/> from queue import queue, empty <br/> class pooledthreadtcpserver (tcpserver, object ): <br/> def _ init _ (self, server_address, requesthandlerclass, thread_num): <br/> super (pooledthreadtcpserver, self ). _ init _ (server_address, requesthandlerclass) <br/> self. _ thread_num = thread_num <br/> self. _ queue = Queue () </P> <p> def _ thread_main (Self): <br/> while self. _ serving: <br/> try: <br/> request, client_address = self. _ queue. get (true, 0.5) <br/> failed t empty: <br/> continue <br/> self. process_request_thread (request, client_address) <br/> dB. close () </P> <p> def process_request_thread (self, request, client_address): <br/> try: <br/> self. finish_request (request, client_address) <br/> response T: <br/> self. handle_error (request, client_address) <br/> finally: <br/> self. close_request (request) </P> <p> def process_request (self, request, client_address): <br/> self. _ queue. put (request, client_address) </P> <p> def serve_forever (Self): <br/> self. _ serving = true <br/> for I in range (self. _ thread_num): <br/> T = thread (target = self. _ thread_main) <br/> T. setdaemon (true) <br/> T. start () <br/> super (pooledthreadtcpserver, self ). serve_forever () <br/>
Inherited from tcpserver and object.
The reason for inheriting objects is that you need to use super to call the method of the parent class and use the new style class.
The process_request method is called when a request arrives.
It puts the request and client address in a queue.
With a requesthandler, you can easily implement a thread pool version of tcpserver
Class echorequesthandler (baserequesthandler): <br/> def handle (Self): <br/> DATA = self. request. recv (1024) <br/> If data = "quit": <br/> self. server. shutdown () <br/> self. request. send (data) <br/> If _ name _ = "_ main _": <br/> ADDR = ("localhost", 12345) <br/> Server = pooledthreadtcpserver (ADDR, echorequesthandler, 10) <br/> server. serve_forever () <br/>