Poll() Method In Python

Method Name:




  • Using select() the number of descriptors that can be monitored is dependent on the FD_SETSIZE of the platform which is normally set to 1024. If a network program has to monitor a number of descriptors beyond FD_SETSIZE, select() can not be used.


  • Using the method poll() there is no limit on the number of descriptors.


  • poll() is event based.


  • Using poll() a set of I/O events can be registered for a descriptor through the register() function.


  • A call to method poll(timeout) will return a list of tuples where each tuple is comprised of a file descriptor and the event. If no event occurred within the time as specified by timeout, an empty list will be returned.


  • Iterating through the list of tuples, I/O events can be processed for specific descriptors.

Sequence diagram - poll() in python


Not applicable


Return Value:

Returns a Polling object


import select

import socket

import os


serverSocket    = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)


ipAddress   = ''

portNumber  = 20000


serverSocket.bind((ipAddress, portNumber))



pollerObject = select.poll()

pollerObject.register(serverSocket, select.POLLIN)



    fdVsEvent = pollerObject.poll(10000)

    for descriptor, Event in fdVsEvent:

        print("Got an incoming connection request")

        print("Start processing")

        # Do accept() on server socket or read from a client socket


Copyright 2024 © pythontic.com