Last updated
Last updated
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.
select()
To use select
, we must first import the select
module:
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).
select.select()
takes 3 arguments:
A list of file descriptors we want to read from.
A list of file descriptors we want to write to.
A list of file descriptors we want to check for exceptions on.
It also returns a tuple of 3 lists:
A list of file descriptors that are ready to be read from. (from the ones we passed as arguments)
A list of file descriptors that are ready to be written to. (from the ones we passed as arguments)
A list of file descriptors that are have exceptions. (from the ones we passed as arguments)
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.
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.