Multi-thread processing of socketserver and multi-thread processing of socketserver

Source: Internet
Author: User

Multi-thread processing of socketserver and multi-thread processing of socketserver
I. Introduction

SocketServer simplifies the compilation of network servers. When creating a socket, using SocketServer will greatly reduce the creation steps, and SocketServer uses select, which has five classes: BaseServer, TCPServer, UDPServer, UnixStreamServer, and unixw.ramserver. The last four classes are processed synchronously. In addition, the ForkingMixIn and ThreadingMixIn classes support Asynchronization.

  Inheritance of the five classes of SocketServer

 

The SocketServer uses IO multiplexing and "multithreading" and "multi-process" internally to implement the Socket server that processes multiple client requests concurrently. That is, when each client request is connected to the server, the Socket server creates a "Thread" or "process" on the server to process all the requests of the current client.

  

Ii. types provided

First, Server: BaseServer/TCPServer/UDPServer is used to receive customer requests. The TCPServer processes TCP requests, and the UDPServer processes UDP requests. BaserServer is a base class and cannot be used directly. TCPServer inherits from BaseServer and UDPServer inherits from TCPServer.

Second, Handler class: BaseRequestHandler/DatagramRequestHandler/StreamRequestHandler is used to process each customer request. Generally, BaseRequestHandler is used, but StreamRequestHandler/DatagramRequestHandler provides some special functions. The former is used to process stream (TCP) requests, and the latter is used to process datagram (UDP) requests. Server creates a Handler class example every time it receives a customer request to process the request. By default, TCPServer/UDPServer is a single-process, single-threaded model. Each customer request is processed in sequence. Only after a single request is processed can the next request be processed.

Third, the MixIn class: ForkingMixIn/ThreadingMixIn is used to provide the Server with multi-process/multi-thread concurrent processing capabilities. ForkingMixIn is a multi-process model and ThreadingMixin is a multi-threaded model. Specifically, you only need to create a class and inherit both the Server class and MixIn class to automatically obtain the concurrent request processing capability. The module itself directly provides this type.

Class mixing (ForkingMixIn, UDPServer): passclass mixing (ForkingMixIn, TCPServer): passclass mixing (ThreadingMixIn, UDPServer): passclass ThreadingTCPServer (ThreadingMixIn, TCPServer): pass 3

 

3. Create socketserver
  • You must first create a request processing class, and this class must inherit BaseRequestHandle and override the handle method in the parent class.
  • Instantiate a server class and pass the server ip address and the newly created request processing class to the server class.
  • Call the handle_request () or server_forever () method of the server class object to start processing the request.
  • Close request

Server. handle_request () processes only one request

Server. forever () processes multiple requests and continues to execute

Iv. Examples

Simply receive the information sent by the client, convert it to uppercase, and then return it to the client.

Server:

#-*-Coding: UTF-8-*-import socketserverclass MyTCPHandler (socketserver. baseRequestHandler): "" The request handler class for our server. it is instantiated once per connection to the server, and must override the handle () method to implement communication to the client. "def handle (self): while True: # receive client information multiple times # self. request is the TCP socket connected to the client self. data = self. request. recv (1, 1024 ). strip () print ("{} wrote :". format (self. client_address [0]) print (self. data) # just send back the same data, but upper-cased self. request. sendall (self. data. upper () if _ name _ = "_ main _": HOST, PORT = "localhost", 9999 # Create the server, binding to localhost on port 9999 server = socketserver. TCPServer (HOST, PORT), MyTCPHandler) # Activate the server; this will keep running until you # interrupt the program with Ctrl-C server. serve_forever ()

Client:

# -*- coding: UTF-8 -*-import socketclient = socket.socket()client.connect(('localhost', 9999))while True:    msg = input('>>:').strip()    if not msg:        continue    else:        client.send(msg.encode('utf-8'))        upData = client.recv(1024)        print(upData.decode())

V. Multithreading

So far, all our c/s connections can only process one client request at the same time. When multiple client requests are made, they can only be executed after the previous client request is closed, including the above Code. To make the socketserver concurrent, You must select the next multi-concurrency class:

class socketserver.ForkingTCPServerclass socketserver.ForkingUDPServerclass socketserver.ThreadingTCPServerclass socketserver.ThreadingUDPServer

You only need to change the location.

server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)

Server:

import socketserverclass MyTCPHandler(socketserver.BaseRequestHandler):    def handle(self):        while True:            # self.request is the TCP socket connected to the client            self.data = self.request.recv(1024).strip()            print("{} wrote:".format(self.client_address[0]))            print(self.data)            # just send back the same data, but upper-cased            self.request.sendall(self.data.upper())if __name__ == "__main__":    HOST, PORT = "localhost", 9999    server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler)    server.serve_forever()

 

  

Related Article

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.