Python select - select.poll()














































Python select - select.poll()




select.poll()

 

poll() function is a part of select module. This function is available on only on Linux. poll() function of select module is similar to the select() function but the underlying operating system implementation is better and more efficient. This function is not available on windows so the programs created using poll() are not portable to windows like select() which works in both operating systems.

 

poll() function provides better scalability for network servers that services many clients at the same time. poll() scales better because the system call only requires listing the file descriptors of interest, while select() builds a bitmap, turns on bits for the fds of interest, and then afterward the whole bitmap has to be linearly scanned again.

 

In terms of complexity, the select() is O(highest file descriptor), while poll() is O(number of file descriptors).

 

poll() returns a polling object, which supports registering and unregistering file descriptors, and then polling them for I/O events. Python implements poll() with a class that manages the registered data channels being monitored. This class has 4 methods named:

  • register() - Register a file descriptor with the polling object. Future calls to the poll() method will then check whether the file descriptor has any pending I/O events.
  • modify() - Modifies an already registered fd. This has the same effect as register(fd, eventmask).
  • unregister() - Remove a file descriptor being tracked by a polling object.
  • poll() - Polls the set of registered file descriptors and returns a possibly empty list containing (fd, event) 2-tuples for the descriptors that have events or errors to report. 

 

Syntax:

 

poll_object = select.poll()


  • poll_object.register(fd[, eventmask])
  • poll_object.modify(fd, eventmask)
  • poll_object.unregister(fd)
  • poll_object.poll([timeout])

 

  • fd: file descriptor(can be either an integer, or an object with a fileno() method that returns an integer)
  • eventmask: It is an optional bitmask describing the type of events you want to check for, and can be a combination of the constants POLLIN, POLLPRI, and POLLOUT. If not specified, the default value used will check for all 3 types of events. Constants used in eventmask are as follows:

Constant

Explanation

POLLIN

There is data to read

POLLPRI

There is urgent data to read

POLLOUT

Ready for output: writing will not block

POLLERR

Error condition of some sort

POLLHUP

Hung up


POLLRDHUP


Stream socket peer closed connection, or shut down writing half of connection

POLLNVAL

Invalid request: descriptor not open





















  • timeout: It specifies the length of time in milliseconds which the system will wait for events before returning.

 


NOTE - 

  1. Registering a file descriptor that's already registered is not an error and has the same effect as registering the descriptor exactly once.
  2. Attempting to remove a file descriptor that was never registered causes a KeyError exception to be raised.
  3. Attempting to modify a file descriptor that was never registered causes an OSError exception with errno ENOENT to be raised.
  4. In poll_object.poll(), an empty list indicates that the call timed out and no file descriptors had any events to report.

Example Usages:



<-PREV                                                                                                                                               NEXT-> 


Comments