Python Network Programming--socket Advanced (Select/poll/epoll)

Source: Internet
Author: User
Tags epoll

The native socket client is blocked when it connects to the server, that is, when the server calls the Accept method, while the server and the client are blocked when the data is received (called recv). The native socket server can only handle one client request at a time, that is, the server cannot communicate with multiple clients simultaneously, and the service side resource is idle (the server only occupies I/O,CPU idle at this time).

The demand now is that we have multiple clients connecting to the server side and that the server side needs to process requests from multiple clients. Obviously, the native socket does not implement this requirement, so how do we handle it at this time?

Workaround: Adopt the I/O multiplexing mechanism. In Python network programming, the I/O multiplexing mechanism is a mechanism used to solve multiple client connection request server side, and the server side can normally process and respond to the client. In writing, there are 1 mechanisms: You can listen to multiple file descriptors at the same time, once the descriptor is ready, you can notify the program to read and write the corresponding operation.

1 IO The concept of multiplexing

I/O multiplexing refers to the ability to monitor multiple descriptors and, once a descriptor is ready (usually read-ready or write-ready), notifies the program to read and write accordingly.

io multiplexing in 1.1 Linux

(1) Select

Select was first seen in 1983 in 4.2BSD, and it is used by a select () system to monitor arrays of multiple file descriptors, and when select () returns, the ready file descriptor in the array is changed by the kernel to the flag bit. Allows the process to obtain these file descriptors for subsequent read and write operations.

Select is currently supported on almost all platforms, and its good cross-platform support is one of its advantages, and in fact it is now one of the few advantages it has left.

A disadvantage of select is that the maximum number of file descriptors that a single process can monitor is limited to 1024 on Linux, but can be improved by modifying the macro definition or even recompiling the kernel.

In addition, the data structure maintained by select () stores a large number of file descriptors, with the increase in the number of file descriptors, the cost of replication increases linearly. At the same time, because the latency of the network response time makes a large number of TCP connections inactive, but calling select () takes a linear scan of all sockets, so this also wastes some overhead.

(2) Poll

Poll was born in 1986 in System V Release 3, and it does not differ substantially from select in nature, but poll does not have a limit on the maximum number of file descriptors.

The disadvantage of poll and select is that an array containing a large number of file descriptors is copied in between the user state and the kernel's address space, regardless of whether the file descriptor is ready, and its overhead increases linearly as the number of file descriptors increases.

In addition, when select () and poll () file descriptors are ready to tell the process, if the process does not have IO operations on it, the next time you invoke select () and poll (), the file descriptors are reported again, so they generally do not lose the ready message. This approach is called horizontal trigger (level triggered).

(3) Epoll

It was not until Linux2.6 that the kernel directly supported the implementation method, that is, Epoll, which has almost all the advantages of the previous said, is recognized as the best performance of the Linux2.6 multi-channel I/O readiness notification method.

Epoll can support both horizontal and edge triggering (edge triggered, which only tells the process which file descriptor has just become ready, it only says it again, and if we do not take action then it will not be told again, this way is called edge triggering), The performance of edge triggering is theoretically higher, but the code implementation is quite complex.

Epoll also only informs those file descriptors that are ready, and when we call Epoll_wait () to get the ready file descriptor, the return is not the actual descriptor, but a value representing the number of ready descriptors, You just have to go to the Epoll specified array to get the appropriate number of file descriptors, and memory mapping (MMAP) technology is used, which completely eliminates the overhead of copying these file descriptors on system calls.

Another essential improvement is the epoll adoption of event-based readiness notification methods. In Select/poll, the kernel scans all monitored file descriptors only after a certain method is called, and Epoll registers a file descriptor beforehand with Epoll_ctl (), once it is ready based on a file descriptor, The kernel uses a callback mechanism like callback to quickly activate the file descriptor and be notified when the process calls Epoll_wait ().

io multiplexing in 1.2 python

Python has a select module, which provides: Select, poll, Epoll three methods, call the system's Select,poll,epoll, respectively, to achieve IO multiplexing.

Windows Python: Available: Select

Mac Python: Available: Select

Linux Python: Available: Select, poll, Epoll

Python Network Programming--socket Advanced (Select/poll/epoll)

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.