Using Select Listener Terminal Operation instance to process multiple socket client requests concurrently with select: The server uses Select to implement pseudo processing of multiple socket client requests simultaneously: client
Here the socket server is compared to the native socket, and he supports that when a request is no longer sending data, it will not wait but can process other requested data. However, if each request takes longer to take, the server side of the Select version cannot complete the simultaneous operation.
Implementation of socket server-side (Python2) Python3 based on select
Socketserver Module
The Socketserver internally uses IO multiplexing and "multithreading" and "multi-process" to enable the socket service side to process multiple client requests concurrently. That is, when each client requests a connection to the server, the socket server is creating a "thread" or "process" dedicated to all requests from the current client.
Threadingtcpserver
The Soket server implemented by Threadingtcpserver creates a " thread " For each client that is used to interact with the client.
1. Threadingtcpserver Foundation
Using Threadingtcpserver:
- Create a class that inherits from Socketserver.baserequesthandler
- A method called handle must be defined in a class
- Start Threadingtcpserver
Socketserver Implementing Server Clients
2. Threadingtcpserver Source Code Analysis
The class diagram relationships of Threadingtcpserver are as follows:
The internal invocation process is:
- Start the service-side program
- Executes the tcpserver.__init__ method, creates the server-side socket object and binds the IP and port
- Executes the baseserver.__init__ method, assigning a custom inherited class Myrequesthandle from Socketserver.baserequesthandler to self. Requesthandlerclass
- Executes the Baseserver.server_forever method, while the loop is always listening for client requests to arrive ...
- When a client connection arrives at the server
- Executes the Threadingmixin.process_request method, creating a "thread" to handle the request
- Execute the Threadingmixin.process_request_thread method
- Executes the Baseserver.finish_request method and executes self. Requesthandlerclass () is the construction method that executes the custom Myrequesthandler (automatically calls the constructor of the base class Baserequesthandler, which in turn calls the Handle method of Myrequesthandler)
Threadingtcpserver related source code:
Baseserver TCPServer threadingmixin Threadingtcpserver
RequestHandler related source code
Socketserver.baserequesthandler
Instance:
Server-Side Client
SOURCE Streamlining:
View Code
As you can see from the streamlined code, Socketserver's threadingtcpserver can handle requests at the same time thanks to the select and threading two things, It is essentially a server-side create a thread for each client, the current thread to handle the corresponding client's request, so, can support simultaneous n client links (long connection).
python--Network Programming