Poll() method in Python

Method Name:

poll()

 

Overview:

  • 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

Parameters:

Not applicable

 

Return Value:

Returns a Polling object

Example:

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   = '127.0.0.1'

portNumber  = 20000

 

serverSocket.bind((ipAddress, portNumber))

serverSocket.listen()

 

pollerObject = select.poll()

pollerObject.register(serverSocket, select.POLLIN)

 

while(True):

    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