Python basic-----python process finalization-----IO model

Source: Internet
Author: User

I. IO model

1. IO Model classification

1. Blocking IO--------blocking IO

2. Non-blocking IO------nonblocking IO

3. multiplexed io-------multiplexing

4. Signal-driven IO-------signal driven IO (not used in work, just as an understanding)

5. Asynchronous IO-------Asynchronous IO

2, usually the IO default operation is divided into two stages (default is blocking IO)

1. Prepare to wait for the data phase, which is equivalent to requesting that the operating system have data sent over (Invoke IO operation).

2. Copy the data from the kernel cache to the process cache.

Ii. different IO operations

1. Blocking io (blocking IO)

1. All sockets are blocked IO by default in Linux system,

      

2. How blocking IO works ():

When the user process invokes the Recvfrom system call (the equivalent of a user process sending a request like the operating system), kernel begins the first phase of IO: Preparing the data. For network IO, many times the data has not arrived at the beginning, this time kernel will wait for enough data to come. On this side of the user process, the entire process is blocked. When kernel waits until the data is ready, it copies the data from the kernel to the user's memory, and then kernel returns the result, and the user process removes the block state and re-runs it. Therefore, the blocking IO is characterized by block in both phases of IO execution.

2. Non-blocking IO

1. The Linux operating system can be non-blocking by setting the socket. This is what the process looks like when you perform a read operation on a non-blocking socket.

        

2. How non-blocking IO works:

As you can see, when the user process issues a read operation, if the data in kernel is not ready, it does not block the user process, but returns an error immediately. From the user process point of view, it initiates a read operation and does not need to wait, but immediately gets a result. When the user process determines that the result is an error, it knows that the data is not ready, so it can send the read operation again. Once the data in the kernel is ready and again receives the system call of the user process, it immediately copies the data to the user's memory and then returns. Therefore, the user process is in fact need to constantly actively ask kernel data well no. If the data is not ready, an error is returned. After the process receives the error, it can do something else before initiating the recvform system call. Repeat the above process and iterate through the recvform system calls. This process is often called polling. Poll the kernel data until the data is ready, and then copy the data to the process for data processing. It is important to note that the process of copying data is still a blocking state.

3. Code principle:

      

3. IO multiplexing

1, the above situation can only be applied to one server processing a customer sock, if you want a server to process multiple customers sock that will be used for IO multiplexing

2. Multiplexing Flowchart:

        

        When the user process invokes select, the entire process is blocked, and at the same time, kernel "monitors" all select-responsible sockets, and when the data in any one socket is ready, select returns. This time the user process then invokes the read operation, copying the data from the kernel to the user process. This figure is not much different from the blocking IO diagram, in fact, it's even worse. Because two system calls (select and Recvfrom) are required, blocking IO only invokes one system call (Recvfrom). However, the advantage of using select is that it can handle multiple connection at the same time, in the IO multiplexing model, the actual, for each socket, is generally set to become non-blocking, but, as shown, The entire user's process is actually always blocked. Only the process is the block of the Select function, not the socket IO.

3. Code implementation principle:

        

4, using selectors to achieve the multi-multiplexing of IO

        

5. Note: Only select blocking in Python is supported under Windows, and all three are supported under Linux. We use the selectors module Python to automatically determine

What blocking method to use.

    

6, each IO model comparison diagram:

        

  

Python basic-----python process finalization-----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.