# Polling Interface

Sometimes you want to monitor multiple file descriptors for input/output at the same time. File descriptors can, for instance, be network sockets. If we are talking about 3 sockets, then having a dedicated thread for each socket is feasible. However, as the number of file descriptors grows, it becomes less possible. Imagine 200 threads trying to monitor 200 sockets!

Instead, we can use the python `select` function, to check which file descriptors have data.

## Using `select()`

To use `select`, we must first import the `select` module:

```python
import select
```

Then, we can use the `select` function, from within the `select` module by calling `select.select()` (not the most intuitive naming, but we must work with it).

### Arguments & Returns

`select.select()` takes 3 arguments:

1. A list of file descriptors we want to read from.
2. A list of file descriptors we want to write to.
3. A list of file descriptors we want to check for exceptions on.

It also returns a tuple of 3 lists:

1. A list of file descriptors that are ***ready*** to be read from. (from the ones we passed as arguments)
2. A list of file descriptors that are ***ready*** to be written to. (from the ones we passed as arguments)
3. A list of file descriptors that are ***have*** exceptions. (from the ones we passed as arguments)

## Example

Assume we wanted to monitor a list of client sockets. We are waiting for messages from them, and we will print them to the screen when they arrive.&#x20;

```python
import select
import socket

clients = []

# Assume the connect_clients function accepts incoming connections
# and returns all of them as a list.
clients.append(connect_clients()) 

while True:
    rdlist, wrlist, exlist = select.select(clients, [], [])
    for client in rdlist:
        message = client.recv(4096).decode("utf-8")
        print(message)
```

This code will connect some clients (ignore how this may work, simply assume it does) and return a list of sockets, corresponding to the connected clients. Then, it will begin looping and waiting for data. You will notice that we pass an empty list for some of the `select.select()` arguments, since we are not interested in writing or exceptions. Once `select.select()` returns, we loop through the ready-to-read sockets and print their messages.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://computer-networks-lab.gitbook.io/computer-networks-lab-manual/appendix/polling-interface.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
