Concept of three multiplexed models select Select Principle
Select is a system called to monitor an array of multiple file descriptors (Files descriptor), and when Select () returns, the ready file descriptor in the array is modified by the kernel to mark bits (in fact, an integer). Allows the process to obtain these file descriptors for subsequent read and write operations. Select Ornaments Monitor the entire array through traversal , and each traversal is linear .
Select advantages
Select is currently supported on almost all platforms and is well-cross-platform in nature.
Select disadvantage
- Each time a select is called, the FD collection needs to be copied from the user state to the kernel state, which can be very expensive when FD is large.
- The maximum number of FD that a single process can monitor is the default of 1024 on Linux (this limit can be improved by modifying the macro definition or recompiling the kernel)
- And because the FD of select is placed in the array, and each time the entire array is linearly traversed, when there are many FD, the overhead is very high
Python Select
The function that calls Select is R, W, E = select.select(rlist, wlist, xlist[, timeout])
, the first three parameters are three lists, the objects in the array are waitable object
: Both the file descriptor of the whole number, or an object that has the method of returning the file descriptor (descriptor) fileno()
;
rlist
: Waiting for the list to be read ready
wlist
: Waiting for the list to be written ready
errlist
: Wait for the list of "exceptions"
select方法用来监视文件描述符,如果文件描述符发生变化,则获取该描述符。
1. These three lists can be an empty list, but receiving 3 empty lists is dependent on the system (acceptable on Linux, but not on Windows).
2、当 rlist
序列中的描述符发生可读时(accetp和read),则获取发生变化的描述符并添加到 r
序列中
3、当 wlist
序列中含有描述符时,则将该序列中所有的描述符添加到 w
序列中
4、当 errlist
序列中的句柄发生错误时,则将该发生错误的句柄添加到 e
序列中
5、当 超时时间 未设置,则select会一直阻塞,直到监听的描述符发生变化
当 超时时间 =
1
时,那么如果监听的句柄均无任何变化,则select会阻塞
1
秒,之后返回三个空列表,如果监听的描述符(fd)有变化,则直接执行。
6, in the list can accept the Ptython
file
objects, such as
sys.stdin
, or it will be
open()
And
os.open()
Return object), the socket object will return
socket.socket()
。 You can also customize the class as long as there is a suitable
fileno()
Method (need to actually return a file descriptor instead of a random integer).
Python IO multiplexing Select poll Epoll