11.python Concurrency Primer (part14 blocking I/O and non-blocking I/O and introducing I/OS multiplexing)

Source: Internet
Author: User
Tags epoll

First, understand what is the I/O model preliminarily.

1. Review the user Configuration and kernel state.

The operating system sits between the application and the hardware, essentially a software that consists of the kernel and system calls.

Kernel: Used to run in the kernel state, the primary role is to manage hardware resources.

System invocation: Runs with the user state, providing an interface for the application to invoke the system.

The core of the operating system is the kernel, the kernel has access to the underlying hardware devices, in order to ensure that the user can not directly operate on the kernel, and ensure the security of the kernel, so the user space and kernel space is divided.


2. Review the process switch.

If you want to implement the transition between processes, then the process needs to have the ability to suspend, have the ability to reply, so it is called switching.

The switch between process and process is done by the operating system!


If there are multiple processes, and one of them is running on the CPU, what happens between the switch to another thread now?

The first step: Save the CPU context, program counters, and registers.

The second step: Update the PCB information, the process of the PCB into the corresponding queue, such as ready, in an event blocking queue, select another process to execute, and update its PCB.

Step three: Update the data structure of the memory management.

Fourth step: Restore the processor context

#这几步理解不了无所谓 .... In fact, the main thing is to explain that the process and the process of switching between really very resource-consuming!!!


3. Review the process blocking.

The process being executed, the process is waiting for the data, or the request resource fails, or the wait for something does not happen.

The system will change the status of this process to block.

Blocking, or it can be said to be a process of their own active behavior.

CPU resources are not available until the process is running, and CPU resources are not consumed if a process becomes blocked. (Blocking does not consume CPU resources at all.) )


4. Review the file descriptor.

Personally think of this kind of file descriptor, only in the Unix,linux (Unix-like system) will exist.

is a very abstract concept used to refer to a file.

The representation of a file descriptor, which is a non-negative integer.

I've always interpreted the file descriptor as an index that points to a single record table for each file opened by each process.

When an application or a process opens or creates a file, the kernel returns a file descriptor.

Once the Linux file descriptor is exhausted, it can cause some applications to not run properly, unable to create socket files, etc...



5. Cache I/O. (data is copied back and forth between the kernel State and the user state)

Take the socket communication for example, the two machines through the socket to communicate, when computer A to computer B send a data, is sent directly to Computer B? In fact, computer A in the transmission of data, the first from the user state to the kernel state, and then the data to their own network card, and then send the external.

The receiving end is transferred from the kernel state to the user state when the data is received.

The data is copied into the buffer of the operating system kernel before it is copied from the buffer of the operating system kernel to the application's address space. User space does not have direct access to kernel space. (It can be understood that the data is copied from the kernel state to the user state)


The above example shows that the cache I/O is a disadvantage, the data in the process of transmission, the data needs to be in the application address space (user state), and kernel space (kernel state) in the back and forth data copy operation! The overhead of CPU and memory is very large!


A brief introduction to the 6.I/O model.

What exactly is an I/O model? What is the difference between the different I/O models?

Take the socket network communication for example, socket communication is a network I/O, the execution of accept (waiting for the connection) and recv (waiting for data reception), will involve two system objects, one is called this I/O operation of the thread or process, and the system kernel.

    1. Wait for the data to be ready.

    2. Then copy the data from the kernel space into the process

The following summarizes the four more commonly used I/O models:

(1). Blocking io (blocking IO) (unix/linux default one I/O model)

Under Linux and Unix-like systems, all socket objects are blocking I/O models, and the following is a running flow of a blocking I/O model.

650) this.width=650; "src=" Http://hi.csdn.net/attachment/201007/31/0_1280550787I2K8.gif "/>

Assuming there are two hosts, host A and Host B to communicate through the socket, host a now executes an accept () to wait for Host B to connect, this time the program Accepet (this process is to request data from the operating system!) ), a system call "Recvfrom" to the kernel, when host A to execute the Accept, Host B to connect to host a? When will host B connect to host a? These are unknown, this time, there is a wait for the data phenomenon, this wait is caused by the kernel of the operating system, the kernel will be waiting for data, the waiting process is blocked!! Host A's code will be stuck in the accept location, unable to continue to execute the following content (has been waiting here).

At this point, Host B performs connect and connects to host A, where the kernel gets the data! (Attention required!) This is the kernel that gets the data! The program cannot directly get the data placed in the kernel space, so the kernel will then copy the data to the user address space. At this moment!! Our program will actually get this data!

This is a running process for blocking IO.

If it's a little more simple:

For network IO, there are times when the data has not arrived at the beginning (for example, a full UDP packet has not been received), and kernel waits for enough data to arrive. 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.

(Only one system call was initiated during the whole process)

(Blocking Io is synchronous,)

(This I/O model is inefficient, but the data can be processed in a timely manner.) )


(2). Nonbloking io (non-blocking IO)

Other systems are not very clear, but under the Linux system, you can set the socket (Setblocking method), you can change the blocking model of the socket as a non-blocking model, the non-blocking model running flow is as follows:

650) this.width=650; "src=" Http://hi.csdn.net/attachment/201007/31/0_128055089469yL.gif "/>

Also take the previous example of host A and Host B communication, host A and Host B to communicate through the socket, host a now performs an accept () to wait for Host B to connect, this will initiate a recvfrom system call, At this point if the kernel has not received a connection to host B, or the data is not ready, this will not block the process, but instead of returning an error (IO blocking exception) (host a side of the program will not wait, but will directly return results). At this time the user's program to make a judgment, judge the result of this recvfrom system call is not error, if it is error (I/O blocking exception) that the data is not ready, but this program will not be blocked, but also to do something else, can also accept or recv (initiate recvfrom system call), once the data in the kernel is ready, and once again received the program's system call, then the data will be copied from the kernel memory space directly to the user's memory space.

So, with a non-blocking IO model, the user's process needs to keep asking the kernel whether it has the data ready.


In Network I/O, using a non-blocking I/O model, initiating recvfrom This system call, the process is not blocked, the kernel directly returns the result to the process, if the data is not ready to return an error, the process after returning, can do something else, and then initiate the Recvform system call, Always repeat the process above.

Cyclic 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.

Finally, there is a point to note!!! Non-blocking I/O model, not the whole process of non-blocking, the kernel to the user control to copy data, this time although very short!! But this process must be blocked!!!!

The following is an example of a non-blocking model of network I/O:

Service side:

Import time

Import socket

SK = Socket.socket (socket.af_inet,socket. SOCK_STREAM)

Sk.setsockopt

Sk.bind (' 127.0.0.1 ', 6667)

Sk.listen (5)

Sk.setblocking (False) #设置非阻塞I/O model

While True:

Try

Print (' Waiting client connection ... ')

Connection,address = Sk.accept () # Process Active Polling

Print ("+ + +", address)

Client_messge = CONNECTION.RECV (1024)

Print (str (client_messge, ' UTF8 '))

Connection.close ()

Except Exception as E:

Print (e)

Time.sleep (4)

Client:

Import time

Import socket

SK = Socket.socket (socket.af_inet,socket. SOCK_STREAM)

While True:

Sk.connect (' 127.0.0.1 ', 6667)

Print ("Hello")

Sk.sendall (bytes ("Hello", "UTF8"))

Time.sleep (2)

Break

In fact, this non-blocking I/O model appears to be highly efficient, but it also has drawbacks:

First, this non-blocking I/O model sends a lot of system calls.

Second, the data can not be processed in a timely manner, first to look at the above example, each polling the kernel data, if the kernel is not ready to find the data, the need to sleep 4 seconds, in the sleep of this 4 seconds, just sleep two seconds, the data in the kernel is ready, but then sleep just executed for 2 seconds, There are 2 seconds without sleep, need to wait two seconds, will be new to poll check the core data (that is, want to get the data, but also over two seconds), so that the data can not be processed in time.



(3). IO multiplexing (I/O multiplexing)

The benefit of I/O multiplexing is that a single process can handle multiple network connections at the same time I/O, in fact, select/epoll these two functions to constantly poll all sockets, once the state of the socket changes, it will notify the user process (program).

The basic operating flowchart for I/O multiplexing is as follows:

650) this.width=650; "src=" Http://hi.csdn.net/attachment/201007/31/0_1280551028YEeQ.gif "/>

First, the SELECT function to initiate a system call, this system call is also called SELECT, one but call this select, the entire process will go into a blocking state, at this time, the kernel back to detect, all select responsible socket, when select responsible for these sockets, If a socket's state changes, then select returns immediately. The user can receive data from this socket.

In fact, select this diagram and blocking I/O is very similar, seemingly no difference, but, using Select is advantageous, select can handle multiple connections at the same time.

If the number of concurrent connections processed is not very high, using this IO multiplexing may not have the performance of multi-threading +io blocking, and may even increase latency.

Attention!! The advantage of IO multiplexing Select/epoll is not that a single connection can be processed faster, but that more connections can be processed.


In the I/O multiplexing mode, each socket is generally set to non-blocking, but the entire user's process is actually blocked! Unlike the default I/O blocking, this blocking is due to the fact that the process is caused by the Select function lock, and the blocking caused by I/O is not the same thing.

Once the Select function returns a content that can be taken, the process can call Accpet or recv to copy the data in the kernel space into the user space.


You only need to consider using the SELECT function if you are dealing with a particularly numerous number of connections, and if you only process a single connection, you cannot show the benefits of select!

Here is an example of the basic usage of the SELECT function:

Service side:

#!/usr/local/bin/python2.7

#-*-Coding:utf-8-*-

Import socket

Import Select

Socket_obj = Socket.socket ()

Socket_obj.bind (("127.0.0.1", 8889))

Socket_obj.listen (5)

While True:

R,w,e = Select.select ([socket_obj,],[],[],5) #用来检测socket_obj这个socket对象是否产生了变化, if there is a change, return to the variable R, (5 for 5 seconds, Even if the socket doesn't change, the code will execute down! )

#一旦这个socket产生了变化之后, the variable r has content.

For I in R:

print "Conn ok!"

Print (' Wait .... ')

Client:

#!/usr/local/bin/python2.7

#-*-Coding:utf-8-*-

Import socket

Socket_client = Socket.socket ()

Socket_client.connect (("127.0.0.1", 8889))

While True:

INPT = Raw_input (">>>"). Strip ()

Socket_client.send (Inpt.encode ("Utf-8"))

data = SOCKET_CLIENT.RECV (1024)

Print Data.decode ("UTF8")


(It may be interesting to know what the mechanism of select detects the socket object is, in fact, select uses a detection mechanism called "horizontal trigger".) About edge triggering and horizontal triggering are described in detail later. )


(4). Asynchronous IO (asynchronous I/O)

is also a use of not much of the I/O model, the following is the running flowchart.

650) this.width=650; "src=" Http://hi.csdn.net/attachment/201007/31/0_1280551287S777.gif "/>

The user program first initiates a aio_read system call, after initiating the system call, the user program will continue to do other things, when the kernel receives the Aio_read this system call, will be like non-blocking IO, immediately return a result, the kernel will wait for the data is ready to complete, The data is then copied to the memory space, and after the copy is finished, the kernel sends a signal to the user process telling the user that the copy of the data is finished.


Attention!!!

Here, one might confuse the concept of an asynchronous I/O model with a non-blocking I/O model, but the difference between the two I/O models is still significant.

In the case of non-blocking I/O models, the process is not blocked for most of the time, but the process is proactively checking that the data in the kernel is ready, and if the data is ready, a process is required to initiate a recvfrom to copy the data from the kernel space to the user space.


asynchronous I/O model is completely different, it feels more like the user process to the I/O operation directly to the kernel, and so on the kernel to do I/O operations, and then to notify the user process, in this process, the user does not need to check the kernel is ready to prepare the data, and do not need to actively go to the kernel space to copy






This article is from the "Rebirth" blog, make sure to keep this source http://suhaozhi.blog.51cto.com/7272298/1927374

11.python Concurrency Primer (part14 blocking I/O and non-blocking I/O and introducing I/OS multiplexing)

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.