select – Wait for I/O Efficiently¶

select – Wait for I/O Efficiently - Python Module of the Week

select – Wait for I/O Efficiently

Purpose:Wait for notification that an input or output channel is ready.
Available In:1.4 and later

The select module provides access to platform-specific I/O monitoring functions. The most portable interface is the POSIX function select(), which is available on Unix and Windows. The module also includes poll(), a Unix-only API, and several options that only work with specific variants of Unix.

select()

Python’s select() function is a direct interface to the underlying operating system implementation. It monitors sockets, open files, and pipes (anything with a fileno() method that returns a valid file descriptor) until they become readable or writable, or a communication error occurs. select() makes it easier to monitor multiple connections at the same time, and is more efficient than writing a polling loop in Python using socket timeouts, because the monitoring happens in the operating system network layer, instead of the interpreter.

Note

Using Python’s file objects with select() works for Unix, but is not supported under Windows.

The echo server example from the socket section can be extended to watch for more than one connection at a time by using select(). The new version starts out by creating a non-blocking TCP/IP socket and configuring it to listen on an address.

原文地址:https://www.cnblogs.com/lexus/p/2843215.html