Python IO Model

Source: Internet
Author: User
Tags epoll

Detail with diagram

The first IO model
Blocking

The second IO model
Non-blocking IO
principle: change from blocking to non-blocking, come back every once in a while (see once, the kernel will send a system call), if nothing else (for small amount of code), the process actively polling
When the server-side socket is setblocking (false), the socket is a non-blocking socket, and when the client link is not received, the exception is triggered directly (proving that the CPU is idle at this stage and can do other things). So other tasks can be handled in exception handling.

Disadvantages
Sending too many system calls
Data is not processed in a timely manner.

The third model of IO
IO multiplexing (single-threaded implementation concurrency with IO idle time for concurrency) (SELECT, Poll,epoll)
Select
The Select function is the system call interface. and select can also block,
But the benefit is that multiple links can be monitored at the same time, which determines why it is so popular.
This is still a mystery, but you can add the conn returned by Socket.accpet () to the listener list, and you'll see. Because there is so much listening, there is always a kernel-state data, which means that it can always be manipulated.
Note: By default, if the kernel-state data is not taken to the user state, the socket is always present in the readable list that is returned. And no more than 1024 of the listening.

#Select simulates a socket server, noting that the socket must be non-blocking for IO multiplexing. #Here 's an example of how select handles multiple non-blocking socket connections at the same time through a single process implementation. #Server SideImportSelectImportSocketImportQueueserver=socket.socket () Server.bind ('localhost', 9000)) Server.listen (1000) server.setblocking (False)#set to non-blocking mode, both accept and recv are non-blocking#here if the direct server.accept (), if no connection will be error, so there is data to tune them#Blockioerror:[winerror 10035] a non-blocking socket operation cannot be completed immediately. Msg_dic ={}inputs= [Server,]#to the list of kernel, select detections. #there must be a value for select to detect, otherwise the error provides invalid parameters. #no other connection before, oneself is a socket, oneself is a connection, detect oneself. Event description There are linksoutputs = []#What do you put in there, next time you're out ? whiletrue:readable, writeable, exceptional= Select.select (inputs, outputs, inputs)#Definition Detection    #new to connect Detect list exception (break)    #the exception is also inputs: detecting the presence of those connections is abnormal    Print(readable,writeable,exceptional) forRinchReadable:ifR isServer#There is data that represents a new connectionconn, addr =server.accept ()Print("There 's a new connection .", addr) inputs.append (conn)#Add the connection to the test list, if the connection is active, it means the data is coming.            #inputs = [Server.conn] # "conn" returns only active connections, but how to determine who is active            #If the server is active, a new connection is made, and the Conn activity comes to the dataMsg_dic[conn] = queue. Queue ()#Initializes a queue, followed by the data to be returned to the client        Else:            Try: Data= R.RECV (1024)#Note that this is R, not conn, where multiple connections are                Print("Receive Data", data)#r.send (data) # can not be sent directly, if the client does not receive, the information is goneMsg_dic[r].put (data)#put the data inside .Outputs.append (R)#into the returned connection queue.            exceptConnectionreseterror as E:Print("the client disconnected", R)ifRinchOutputs:outputs.remove (R)#clean a disconnected connectionInputs.remove (R)#clean a disconnected connection                delMSG_DIC[R]##清理已断开的连接     forWinchWriteable:#List of connections to be returned to the clientData_to_client = Msg_dic[w].get ()#fetch data in a dictionaryW.send (data_to_client)#Return to clientOutputs.remove (W)#Delete this data to make sure the next loop does not return the processed connection.      forEinchExceptional:#Delete connection-related data if the connection is broken        ifEinchOutputs:outputs.remove (E) inputs.remove (e)delMsg_dic[e]#*************************clientImportsocketclient=socket.socket () Client.connect ('localhost', 9000)) whileTrue:cmd= Input ('>>>'). Strip ()ifLen (cmd) = = 0:Continueclient.send (Cmd.encode ('Utf-8')) Data= CLIENT.RECV (1024)    Print(Data.decode ()) Client.close ()

Poll
Compared to select, a point is to increase the maximum number of links

Epoll
The Epoll implementation principle is the same as select (schematic), but the implementation mechanism is different (that is, the data came, how to find the specified socket)

The Select internal principle is to take polling , like, in a classroom where the teacher hears a student take the table, but doesn't know who took it,
Only one question. So a lot of time is spent on the round of inquiry (it is every time there is data to be polled)
Epoll Internal principle is tell , like, students do not shoot the table, directly stand up who I am, I want to reverse
Ngexi Server Bottom is epoll, multi-process multithreading is not much open, but a thread inside through the Epoll to achieve multi-connection

Asynchronous IO

Async is the whole process of not having a bit of blocking. You do other things when you finish the request, wait until the kernel receives the data and copies it to the user state, and then sends you a signal.
So the above IO multiplexing is not asynchronous.

Synchronous IO
There is a blocking before the IO operation is complete. and asynchronous Io is no block. So multiplexing is all part of the synchronous IO

Python IO Model

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.