IO Model--blocking IO, non-blocking Io,io multiplexing, asynchronous IO

Source: Internet
Author: User

IO Model Description:

* Blocking IO blocking IO
* nonblocking io non-blocking IO
* IO multiplexing IO multiplexing
* Signal driven IO signal driver io ()
* Asynchronous IO Asynchronous IO

IO Model Description:

 To better understand the IO model, we need to review it in advance: synchronous, asynchronous, blocking, non-blocking

What is the difference between synchronous (synchronous) IO and asynchronous (asynchronous) Io, what is blocking (blocking) IO and non-blocking (non-blocking) IO respectively? The problem is that different people may give different answers, such as wikis, that asynchronous IO and non-blocking io are a thing. This is because different people have different backgrounds, and the context is not the same when discussing this issue. Therefore, in order to better answer this question, I first limit the context of this article.

This article discusses the background of network IO in a Linux environment. The most important reference in this document is Richard Stevens's UNIX? Network Programming Volume 1, third edition:the Sockets Networking ", 6.2 section" I/O Models ", Stevens in this section detailing the features and differences of various IO, if English is good enough , it is recommended to read directly. The style of Stevens is famous, so don't worry about it. The flowchart in this paper is also intercepted from the reference literature.

Stevens compared five IO Model in the article:
* Blocking IO blocking IO
* nonblocking io non-blocking IO
* IO multiplexing IO multiplexing
* Signal driven IO signal driver IO
* Asynchronous IO Asynchronous IO
By signal driven IO (signal driven IO) is not commonly used in practice, so the main introduction of the remaining four IO Model.

Again, the objects and steps involved in the IO occur. For a network IO (here we read for example), it involves two system objects, one that calls the IO process (or thread), and the other is the system kernel (kernel). When a read operation occurs, the operation goes through two stages:

#1) wait for data preparation (waiting for the Copying) to copy the data from the kernel into the process (the kernel to the procedure)

It is important to remember these two points, because the difference between these IO models is that there are different situations in both phases.

# synchronization: After a task is submitted, wait for the task to finish executing       # like going to the bank to save money, to take out the number to save # Asynchronous: Just submit the task, do not wait for the task to complete, you can do other things        # You can save money and play with your phone # blocking: recv recvfrom accpt    number, waiting for a lot of people # non-blocking: #很多种情况了 #recv waiting for data preparation, Wait for the data to be copied from the kernel to the process #Send totell the user to send the message copy to the operating system  when sending the network delay        #  The active side so the blockage is very small
Small Understanding

Miss King notes: Io multiplexing

One. Blocking IO:

# figure in Youdao dictionary 20181001 io Model # 1.TCP/UDP recv-System call-kernel OS data is not ready-wait for data-------> data is ready, copy data, copying data from the operating system, and returning to process # two important nodes: 1. Data preparation, 2. Copying information from the kernel to the process # blocking IO Features: The program is blocked and cannot receive other tasks # processes and threads, which do not resolve the blocking time, are just opening multiple processes/threads, which are still waiting, each thread is affected by blocking # The process only solves this problem to some extent, but still waits for

Two. Non-blocking IO:

#figure in Youdao dictionary 20181001 io Model#non-blocking IO#Client side:#recv----> Tell the system I want to collect data----> system tells you no data----> then the program does not block, can continue to execute, but also did not receive data#after a while, continue to cross-examine#recv----> Then the system continues to tell you no data, repeat the process above#Suppose, had been requested, and requested many times, until the#recv----> have data,----> Copy to process from the operating system, and then tell the user that there is data#but non-blocking IO models are never recommended. #we cannot otherwise have the advantage of being able to do other things while waiting for the task to be completed (including submitting other tasks, that is, "backstage" can have multiple tasks at "" and ""). #but it's also hard to hide its drawbacks:    #1. Cyclic call recv () will significantly push up the CPU occupancy rate; This is why we leave a sentence of Time.sleep (2) in the code, otherwise it is very easy to appear in the low-match host machine condition    #2. The Response latency for task completion is increased, because each time a read operation is polled, the task may be completed at any time between two polls. This can result in a decrease in overall data throughput.     #In addition, in this scenario, recv () is more of a function of detecting whether "operation is complete",    #The actual operating system provides a more efficient interface for detecting whether "operation is done", such as select () multiplexing mode, which can detect multiple connections at once. 

Non-blocking IO framework, examples and explanations:

ImportSocketsk=socket.socket () Sk.bind ('127.0.0.1', 8088) ) sk.setblocking (False)#~~~~~~~~~~~~~~ block is set to not blockSk.listen () conn_lst=[]del_lst= [] whileTrue:Try: Conn,addr=sk.accept ()Print('established a connection', addr) conn_lst.append (conn)#msg = CONN.RECV (1024x768). Decode (' UTF8 ') #注释1        #Print (msg)    exceptBlockingioerror: forConinchConn_lst:#loop to get con repeatedly            Try: Msg= CON.RECV (1024)#This is still non-blocking, or it will be an error.                ifmsg = = B"':#However, if you keep getting the data from a closed client, you will always print an emptyDel_lst.append (Con)#cannot conn_lst.remove (con) no longer for the elements of the list in the For Loop, index will be wrong                    Continue #when con passes over to be empty, it does not execute the following statement, to proceed to the next loop                Print(msg) con.send (b'Bye')            exceptBlockingioerror:Pass         forConinchdel_lst:con.close () Conn_lst.remove (Con) del_lst.clear ()#Note 1:    #Here are two cases, the first because I am not blocking at this time (setblocking) client side to chat with me very quickly, resulting in a while loop is not effective, others can not receive the request    #But if only a quick word, and then the server can only receive this sentence, and then because of the non-blocking, and then this conn by memory to wash away    #The second because it is blocked at this time, the client if a little bit slower to send me a message, I will immediately skip, and error Blockingioerror#Workaround:    #set up a conn list and put the conn of each successful connection into the list. And put recv into the except statement, because no information will be error.     #then for loop this conn list, always try to get, conn whether to send a message come over    #so that the original memory brush out of the conn, can always be tried to obtain    #But this is the list for the For loop, recv is still non-blocking, so continue exception handling
Server Side
Import TimeImportSocketImportThreadingdeffunc (): SK=socket.socket () Sk.connect ('127.0.0.1', 8088)) Sk.send (b'Hello') Time.sleep (0.1)    Print(SK.RECV (1024) ) Sk.close () forIinchRange (20): Threading. Thread (Target=func). Start ()
Client Side

Three. IO multiplexing: (select module)

#Select Module#figure in Youdao#IO multiplexing: OS-level, Windows's select mechanism, not provided by our code#Process:#1. There is an agent (select module) that can help you listen to an object. And this agent can listen to multiple objects conn1,2,3,4 ...    #objects that can accept data, such as Socket.accept conn.recv    #A system call occurs,#2. Then this agent will wait for you to be connected to the socket, if no one to connect, will always block (so that the listener can not block the object)#3. Block to have the client to connect, and then the feedback information to help listen to the object, such as Accept    #2-3 Steps, is the operating system to help you loop the listening list, to see whether each item has a readable event (but not very efficient)#4. Then accept that there is data to come, once again generate a system call, find the operating system to data#5. The system replicates the data and transmits it to the first object to ask for data .#Note: The first step is IO multiplexing, which is the step for the object to get the data normally.#Listening mode:    #Windows has only select mechanism    #Linux:        #Select mechanism: The operating system polls each monitored object to see if there is a read operation        #poll mechanism: Same as the select mechanism, but poll is good at listening to the object, more than the select mechanism                    #For example, select can listen to 500, poll can listen to 1000                    #as the number of listening items increases, the monitoring efficiency is reduced. (for example, there are 1000 listening lists, when I listen to the 3rd one, the 2nd one comes to the news,)        #epoll mechanism:                    #to each listener object, a callback function is bound                    #whenever there is a readable event, the callback function immediately feeds back without waiting for polling                    #very efficient and can be used on Linux                    #selectors module, automatically help you choose, your most suitable monitoring mechanism, do not need to care

IO multiplexing Framework, examples and explanations:

ImportSocketImportSelectsk=socket.socket () Sk.bind ('127.0.0.1', 8080) ) sk.setblocking (False) sk.listen () Read_lst=[SK] whileTrue:r_lst,w_lst,x_lst= Select.select (read_lst,[],[])#Read_lst in the listener, only the response, will appear in the R_lst    Print('***', R_lst)#Note 1     forIinchR_lst:ifI issk:conn,addr=sk.accept () read_lst.append (conn)Else: Ret= I.RECV (1024)            ifret = = b"': I.close () read_lst.remove (i)#at this time R_lst and Read_lst are two different memory spaces, you can remove                Continue            Print(ret) i.send (b'Goodbye')#Note 1    #at this time after a connection, Read_lst originally had [Conn,sk], and then if someone sent me a message, that is, I am conn.recv at this time    #because the Select module, at this time the R_LST only Conn, because no one to connect me, SK will not appear in the R_lst
Server Side
Import TimeImportSocketImportThreadingdeffunc (): SK=socket.socket () Sk.connect ('127.0.0.1', 8080)) Sk.send (b'Hello') Time.sleep (3)    Print(SK.RECV (1024) ) Sk.close () forIinchRange (20): Threading. Thread (Target=func). Start ()
Client Side

Four. Asynchronous IO:

#Asynchronous IO#Steps    #1. Block the object, Recv,accept, tell the system, I want the data, and then go do something else, don't block    #2. The operating system is over there, waiting for data to wait until there's data coming.    #3. The operating system then transmits the data directly to the user.             #(!! But Python does not provide this copy data in this step without providing a Python interface to the operating system            #so it can't be implemented with Python code, the real asynchronous IO model            #But C can, we can use many asynchronous frameworks to implement    #4. The user receives the data. #Django is not an asynchronous framework#Asynchronous framework: can respond to more requests without the blocking phase of Waitdata and CopyData    #Twisted Frame    #Tornado Frame    #Tornado and twisted, as asynchronous frameworks, are very similar.     #just tornado lightweight, twisted some weight. In other respects, it is also the length of each other.     #after the measurement, found that the two frameworks, I/O performance is similar, the use of computing resources is much different!     #It is recommended to use twisted if the overall performance is pursued. 

IO Model--blocking IO, non-blocking Io,io multiplexing, asynchronous IO

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.