I would like to know how the selector module works and how can I use it in socket programming. What does the selector.register() do? I've searched a lot but couldn't find anything, so I couldn't wrap my head around it.
The selector module is a higher level interface around the good old select function. That select function was invented in the 80's for the Unix BSD 4.2 OS, along with the other parts of the Berkeley sockets interface. And as the rest of the socket interface it is still in use nowadays to allow a single execution thread to handle a number of incoming and outgoing connections => it is the best way I know to handle large network throughput using limited resources.
The underlying engine of select is to present the readiness state (both for reading and writing) of a list of interfaces. From that on, it is the responsability of the application (and of the application programmer) to use that information to only read or write to a socket when the operation cannot block. Not that hard, but the threading model (one execution thread per connection) if far more natural for a human being (what most programmers are except for gods).
The Python selector module presents an event driven interface above select:
Functionaly it is exactly the same of the good old select module. But it is much more programmer friendly with all the OOP goodies and the event driven pattern. Old dinausors, that were used to programming select in C language may find that it is not really worth learning a new interface. But IMHO, the fact is that it allows to write code easier to read and test.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With