Python I/O model
What is the difference between synchronous IO and asynchronous Io, what is blocking IO and non-blocking IO respectively? The answers given by different people in different contexts are different. So first limit the context of this article.
Concept Description
Before interpreting, there are a few concepts to be explained:
- User space and kernel space
- Process switching
- Blocking of processes
- File descriptor
- Cache I/O
User space and kernel space
Now that the operating system is using virtual memory, the 32-bit operating system, its addressing space (virtual storage space) is 4G (2 of 32). The core of the operating system is the kernel, which is independent of the normal application, has access to protected memory space, and has all the permissions to access the underlying hardware device. In order to ensure that the user process can not directly manipulate the kernel (kernel), to ensure the security of the kernel, worry about the system to divide the virtual space into two parts, part of the kernel space, part of the user space. For the Linux operating system, the highest 1G bytes (from the virtual address 0xc0000000 to 0xFFFFFFFF) for the kernel to use, called the kernel space, and the lower 3G bytes (from the virtual address 0x00000000 to 0xBFFFFFFF) for each process to use, Called User space.
Process switching
To control the execution of a process, the kernel must have the ability to suspend a process that is running on the CPU and resume execution of a previously suspended process. This behavior is called process switching. So it can be said that any process that runs under the support of the operating system kernel is closely related to the kernel.
The process of moving from one process to another runs through the following changes:
- 1. Save the processor context, including program counters and other registers.
- 2. Update the PCB information.
- 3. Move the PCB of the process into the appropriate queue, such as ready, in an event blocking queue.
- 4. Select another process to execute and update its PCB.
- 5. Update the data structure of the memory management.
- 6. Restore the processing machine context.
Blocking of processes
The executing process, because some expected events did not occur, such as requesting system resources failed, waiting for the completion of an operation, new data has not arrived or no new work to do, etc., the system automatically executes the blocking primitive (block), making itself from the running state into a blocking state. It can be seen that the blocking of a process is an active behavior of the process itself, and therefore it is possible to turn it into a blocking state only if the process is in a running state (acquiring the CPU). 当进程进入阻塞状态,是不占用CPU资源的.
File Descriptor FD
File descriptor, a term in computer science, is an abstraction that describes a reference to a file.
The file descriptor is formally a non-negative integer. In fact, it is an index value that points to the record table in which the kernel opens a file for each process maintained by the process. When a program opens an existing file or creates a new file, the kernel returns a file descriptor to the process. In programming, some of the underlying programming often revolves around file descriptors. However, the concept of file descriptors is often applied only to operating systems such as UNIX and Linux.
Cache I/O
Cache I/O is also known as standard I/O, and most file system default I/O operations are cache I/O. In the Linux cache I/O mechanism, the operating system caches the I/O data in the file system's page cache, which means that the data is copied into the buffer of the operating system kernel before it is copied from the operating system kernel buffer to the application's address space.
Disadvantages of Cache I/O:
Data is required to perform multiple data copy operations in the application address space and the kernel during the transfer process, and the CPU and memory overhead of these data copy operations is very large.
IO mode
Just now, for an IO access (read example), the data is copied to the operating system kernel buffer before it is copied from the operating system kernel buffer to the application's address space. So, when a read operation occurs, it goes through two stages:
1. Wait for data preparation (waiting for the
2. Copying data from the kernel to the process (Copying the data from the kernel to the)
Formally because of these two phases, the Linux system produces the following five kinds of network mode scheme.
-Blocking I/O (blocking IO)
-Non-blocking I/O (nonblocking IO)
-I/O multiplexing (IO multiplexing)
-Signal-driven I/O (signal driven IO)
-Asynchronous I/O (asynchronous IO)
Blocking I/O (blocking IO)
In Linux, all sockets are blocking by default, and a typical read operation flow is probably this:
When the user process invokes the RECVFROM system call, Kernel begins the first phase of IO: Preparing the data (for network IO, many times the data has not arrived at the beginning.) For example, you have not received a full UDP packet. This time kernel will have to wait for enough data to arrive. This process needs to wait, which means that the data is copied into the buffer of the operating system kernel, which requires a process. On this side of the user process, the entire process is blocked (of course, by the process's own choice of blocking). 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.
Non-blocking I/O (nonblocking IO)
Under Linux, you can make it non-blocking by setting the socket. When you perform a read operation on a non-blocking socket, the process looks like this:
When the user process issues a read operation, if the data in the kernel is not yet 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.
I/O multiplexing (IO multiplexing)
Io Multiplexing is what we call Select,poll,epoll, and in some places this IO mode is the event driven IO. The benefit of Select/epoll is that a single process can simultaneously handle multiple network connections of IO. The basic principle of the select,poll,epoll is that the function will constantly poll all sockets that are responsible, and when a socket has data arrives, notifies the user of the process.
当用户进程调用了select,那么整个进程会被block, and at the same time, kernel will "monitor" all the select-responsible sockets, and when the data in any one socket is ready, select will return. 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.
Therefore, if the number of connections processed is not high, Web server using Select/epoll does not necessarily perform better than the Web server using multi-threading + blocking IO, and may be more delayed. The advantage of Select/epoll is not that a single connection can be processed faster, but that it can handle more connections. )
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 block. Only the process is the block of the Select function, not the socket IO.
asynchronous I/O (asynchronous IO)
The asynchronous IO under Inux is actually used very little. Let's take a look at its process:
After the user process initiates the read operation, you can begin to do other things immediately. On the other hand, from the perspective of kernel, when it receives a asynchronous read, first it returns immediately, so no block is generated for the user process. Then, kernel waits for the data to be ready and then copies the data to the user's memory, and when all this is done, kernel sends a signal to the user process to tell it that the read operation is complete.
Summarize the differences between blocking and non-blocking
Calling blocking IO will block the corresponding process until the operation is complete, and non-blocking IO will return immediately when the kernel is ready for the data.
The difference between synchronous IO and asynchronous IO
Before explaining the difference between synchronous IO and asynchronous IO, you need to give a definition of both. The definition of POSIX is this:
-A Synchronous I/O operation causes the requesting process to being blocked until that I/O operation completes;
-an asynchronous I/O operation does not cause the requesting process to be blocked;
The difference is that synchronous IO will block the process when it does "IO operation". According to this definition, the blocking io,non-blocking Io,io Multiplexing described previously are synchronous IO.
Some people will say, non-blocking io is not block AH. Here is a very "tricky" place, defined in the "IO operation" refers to the real IO operation, is the example of recvfrom this system call. Non-blocking IO does not block the process when it executes recvfrom this system call if the kernel data is not ready. However, when the data in the kernel is ready, recvfrom copies the data from the kernel to the user's memory, at which point the process is blocked, during which time the process is block.
The asynchronous IO is not the same, and when the process initiates an IO operation, the direct return is ignored until the kernel sends a signal telling the process that IO is complete. Throughout this process, the process has not been blocked at all.
Comparison of each IO model:
From the above image, it can be found that the difference between non-blocking io and asynchronous IO is still obvious. In non-blocking io, although the process will not be blocked for most of the time, it still requires the process to go to the active check, and when the data is ready, it is also necessary for the process to proactively call Recvfrom to copy the data to the user's memory. and asynchronous Io is completely different. It's like a user process handing over an entire IO operation to someone else (kernel) and then sending a signal notification when someone finishes it. During this time, the user process does not need to check the status of the IO operation, nor does it need to actively copy the data.
I/O multiplexing Select
The select,poll,epoll is a mechanism for IO multiplexing. I/O multiplexing is a mechanism by which a process can monitor multiple descriptors and, once a descriptor is ready (usually read-ready or ready to write), notifies the program to read and write accordingly. But select,poll,epoll are essentially synchronous I/O because they all need to read and write when the read-write event is ready, that is, the read-write process is blocked, and asynchronous I/O is not responsible for reading and writing, and the asynchronous I/O implementation is responsible for copying the data from the kernel to the user space.
Select
int select (int n, fd_set *readfds, Fd_set *writefds, Fd_set *exceptfds, struct timeval *timeout);
The Select function monitors file descriptors in 3 categories, Writefds, Readfds, and Exceptfds, respectively. After the call, the Select function blocks until a description is ready (with data readable, writable, or except), or timed out (timeout Specifies the wait time, and if the return is set to null immediately), the function returns. When the Select function returns, you can find the ready descriptor by traversing Fdset.
Select is currently supported on almost all platforms, and its good cross-platform support is one of its advantages. A disadvantage of select is that the maximum number of file descriptors that a single process can monitor is 1024 on Linux, which can be improved by modifying the macro definition or even recompiling the kernel, but this also results in a decrease in efficiency.
Instance:
Server
#!/usr/bin/env python#-*-coding:utf-8-*-#Author:jerry ShiImportSelect,socket,queueserver=socket.socket () Server.bind ("localhost", 9000)) Server.listen (1000) server.setblocking (False)#non-blockingMsg_dic={}inputs= [Server,]#server,conn,conn2outputs = []#R1,R2 whiletrue:readable,writeable,exceptional=Select.select (inputs,outputs,inputs)Print(readable,writeable,exceptional) forRinchReadable:ifR isServer#represents a new connection.CONN,ADDR =server.accept ()Print(CONN,ADDR) inputs.append (conn)#because the newly established connection has not sent data, now accept the program error. To implement client-side data, the server can know that a select is required to listen. Msg_dic[conn]=queue. Queue ()#initialize a queue to hold the accepted data Else: Data= R.RECV (1024) Print("Receive Data", data) msg_dic[r].put (data)#r.send (data) #print ("Send Done")Outputs.append (R)#put the returned connection queue forWinchWriteable:#List of connections to be returned to the clientData_to_client =Msg_dic[w].get () w.send (data_to_client) outputs.remove (w)#ensure that the next loop does not return a connection that has already been processed forEinchExceptional:#Handling Exceptions ifEinchOutputs:outputs.remove (E) inputs.remove (e)delMsg_dic[e]
Client
#!/usr/bin/env python#-*-coding:utf-8-*-#Author:jerry ShiImportSocketclassTcpclient:host='localhost'PORT= 9000Bufsiz= 1024ADDR=(HOST, PORT)def __init__(self): self.client=Socket.socket (Socket.af_inet,socket. SOCK_STREAM) Self.client.connect (self. ADDR) whileTrue:data= Input ('Please enter:') if notData: Breakself.client.send (Data.encode ('UTF8')) Print('send information to%s:%s'%(self.) HOST, data)) data=Self.client.recv (self. Bufsiz)if notData: Break Print('message received from%s:%s'% (self. HOST, Data.decode ('UTF8')))if __name__=='__main__': Client= TcpClient ()
Basic Python Learning (10)