Socketserver of Thread Pool

Source: Internet
Author: User

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/> 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.