Python io Mode (multiplexing and asynchronous IO in-depth understanding)

Source: Internet
Author: User
Tags epoll

1, event channel model. The event channel is the prototype of asynchronous IO.

2, io mode, one IO call will go through two stages. First, wait for the data phase, the data from the network or disk read to the system kernel (kennel) Two, the data from the kernel copy into the process.

Based on these two phases, the Linux system generates five network mode scenarios.

-Blocking I/O (blocking IO)

-Non-blocking I/O (nobokcing IO)

-I/O multiplexing. (I/O multiplexing)

-Signal driven

-Asynchronous I/O (async)

Due to less signal-driven use, the main introduction of the remaining four modes.

3. Blocking I/O (blocking IO) is blocked during both the data preparation phase and the beta data phase. The user calls Recefrom to wait until the data is copied to the user's memory.

2, non-blocking I/O (nobokcing IO), the user will always call Recefrom if the data is not ready. will return an error. All the time until the data is ready. Therefore, it does not get stuck in the reading data phase.

3, Epoll mode, that is, IO multiplexing mode. Multiple network IO can be processed at once. Call the Select method. First it will get stuck. Until one of them has data, it returns immediately. Then copy the data. And then into the next loop.

4, asynchronous Io, asynchronous I/O is not blocked. After the user process initiates a read operation. You can start doing other things immediately (equivalent to telling the kernel to register an event.) Monitored by the kernel, after processing the kernel proactive notification). On the other hand, from a kennel point of view, when he receives an async read, he will immediately callback. So there will be no blocking. When kennel prepares the data, the data is checked to the user's memory, and the user is informed that the read operation is complete.

5, summary.

Blocking IO and nonblocking io differences:

One, call blocking IO will always block, know that read all completed. The nonblocking IO will return immediately in ready. The same point is that both are stuck in the coy phase.

The difference between async and synch:

The one or two difference is whether the copy data phase will block. So the above blocking io, nonblocking io, I/O multiplexing all belong to the synchronous i/0. And Async does not block at any stage.

6, select poll Epoll difference.

Select: He uses a select () system tune to monitor arrays of multiple file descriptors. When select () returns, the file descriptor is modified by the kernel to represent the bit. Allows the process to obtain a file descriptor. For subsequent read and write operations. There is a maximum limit on the number of file descriptors that are monitored. You can resolve the 2 by modifying the kernel parameters, because select () will perform a linear scan of all sockets. This also wastes a certain amount of overhead. As the file descriptor increases or decreases. The scan time linearly increases.

Poll: The maximum number of files limit is removed. The disadvantage is the same as select. Additionally, if the ready file is told to the background process, the process is not read. The next scan will continue. So the message is generally not lost. Called a horizontal trigger.

Epoll mode. One of the most used ways. Both horizontal and edge triggering are supported (tell the process descriptor to be ready, he said only once.) If the program does not take action. Next time will not tell, this is called Edge trigger). Edge triggering is more complex. Higher performance. Pros: Epoll compared to poll is that each scan scans only the number of active connections. Save on overhead.

Python io Mode (multiplexing and asynchronous IO in-depth understanding)

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.