"Python"--Introduction and implementation of IO multiplexing (SELECT, poll, Epoll)

Source: Internet
Author: User

Introduction and implementation of IO multiplexing (SELECT, poll, Epoll)

Io multiplexing includes SELECT, pool, Epoll, which are synchronous and not asynchronous

I. Introduction of IO multiplexing

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 ().

4, Sellect, poll, epoll three differences

Second, select IO multiplexed code example

The Python Select () method directly invokes the operating system's IO interface, which monitors sockets,open files, and pipes (all file handles with Fileno () methods) become readable and writeable, or communication errors, Select () makes it easy to monitor multiple connections at the same time, and this is more efficient than writing a long loop to wait and monitor a multi-client connection, because select operates directly from the C network interface provided by the operating system, rather than through the Python interpreter.

Import select,socketserver = Socket.socket () server.bind (("localhost", 9000)) Server.listen (+) server.setblocking ( False)  # Set to non-blocking inputs = [Server,]   # Start with your own connection, so start by sending your own connection to the list outputs = []while True:    # Exceptional indicates that if an exception occurs in the inputs list, it is output to this exceptional    readable, writeable, exceptional = select.select (inputs, outputs, inputs)    # Print (readable,writeable,exceptional) for    R in readable:        if R is server:  # Table A new connection            conn, addr = r.accept ()            print ("A new connection is coming", addr)            inputs.append (conn)  # Because this new connection hasn't been sent yet. If you receive it now, the program will error            #所以要想实现这个客户端发数据过来时server端能知道, you need to let select re-detect this conn        else:            data = R.RECV (1024x768)            Print ("Received data:", data)            r.send ("            send done ...")

"Python"--Introduction and implementation of IO multiplexing (SELECT, poll, Epoll)

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.