Python IO multiplexing Select module

Source: Internet
Author: User

Multiplexed Analysis Example: server, Client

#server-side configuration fromSocketImport*Import TimeImportSelectserver=socket (af_inet, Sock_stream) server.bind (('127.0.0.1', 8080)) Server.listen (5) server.setblocking (False)" "The advantage of Select/epoll is not that a single connection can be processed faster, but that it can handle more connections. When the data in any socket is ready, select returns, copying the data from the operating system into the application 1.select.select (read_list,write_list,[],5) is an application-initiated, sent to the operating system, Let the operating system to find the socket sockets Socket 5: Can not write, meaning is 5 seconds if there is a data connection will be executed, if there is no return to execute the next line of code (do not write because if there is no data to perform the following is meaningless, can execute the following code, it must be above the data has successfully established a connection) Point: There is no data to block the data will be running, will not block 2. Asynchronous IO: No tube After sending, will automatically send the results to you, send one to the operating system is not the tube (for crawlers, the most efficient)" "Data_dic={}#set an empty dictionary to be used to bind conn:data one by oneRead_list=[server,]#put and receive messages about Sockets Conn.recv () server.accept (), which are detected by some socketsWrite_list=[]#send write data, store some of the established socket service endPrint('start ....') whileTrue:#The return result of select execution is the list of maintenance, which has data coming up.   Rl,wl,xl=select.select (read_list,write_list,[])  #here select in the specified time period to keep asking the operating system for data, if no data will be executed the following line of code    Print(read_list)#Read_list=[server,conn1,conn2,conn3,conn4]    Print(Rl,len (RL))#focus on understanding: The above cycle every time to get R1 only when the replacement operating system asked to have the data socket [conn1,conn2 ...]    #R1 is the select returned    #print (' read_list:%s rl:%s wl:%s '% (Len (read_list), Len (RL), Len (WL))) #rl =[conn1,conn2]#Note:     #1. Just start the loop R1 inside only Server,read_list=[server] inside also only server, at this time R1 and read_list inside the tube like     #2. Start Read_list=[server,conn] Server is responsible for establishing a new connection, Conn is responsible for receiving the message    #3. When the server accept () receives a new connection and joins the Read_list[server,conn],    #When the result is Rl,wl,xl=select.select (read_list,write_list,[]), the resulting R1 contains only the connections that already have data  .    #read_list inside the connection request will not take away, as long as there is data will not block, blocking the situation is the list of no data to take    #R1 inside to the for loop fetch data, if the server socket object is accept (), otherwise responsible for recv () data     forSkinchRl:#The initial state of the read_list is the server socket, which is required to perform an accept operation        ifSK = = Server: # to determine the for loop, first only the Server service side is responsible for accepting () Conn,addr=sk.accept () read_list.append (conn)#the number of concurrent in the back of the read_list to put some data in the socket object        Else:            #Sk.recv (1024x768)            #print (SK)DATA=SK.RECV (1024) write_list.append (SK)  # There's a message going out so I put it in Data_dic[sk]=data#make a dictionary bound to send the object and data: The format for the sent object conn: the corresponding data sent            #because Conn consumes the resources of the application, the corresponding operating system, and the maintenance connection     forSkinchWl:sk.send (Data_dic[sk].upper ())#according to the corresponding socket socket object in the dictionary to get, get       Data_dic.pop (SK) # After the end of the dictionary there is no need to store the message, so the recovery of Write_list.remove (SK) # after the return to the end, so delete         #Write_list will return to W1 when it's not full.        #read_list Resources for Applications        #because Conn consumes the resources of the application, the corresponding operating system also maintains the link
#Client Configuration fromSocketImport*Importosclient=socket (Af_inet,sock_stream) client.connect (('127.0.0.1', 8080)) whiletrue:msg=input ('Enter the command you want to manipulate:') Client.send (Msg.encode ('Utf-8')) Data=CLIENT.RECV (1024)    Print(Data.decode ('Utf-8'))

Python IO multiplexing Select module

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.