I’m trying to write a class with the same interface of Python 2 Standard Library’s socket.socket.
I’ve problems trying to reproduce the behavior the object should have when a program tries to call select.select() on it.
The documentation in the entry for select.select says:
You may also define a wrapper class yourself, as long as it has an appropriate fileno() method (that really returns a file descriptor, not just a random integer).
I would like to try something like this: creating a file-like object that can be controlled by a thread of my program with select, while another thread of my program can set it when my object is ready for reading and writing. How can I do it?
The fileno() function needs to return a kernel file descriptor, so that it can be passed to the select system call (or poll/epoll/whatever). The multiplexing done by select-like operations is fundamentally an OS operation which must work on OS objects.
If you want to implement this for an object not based on an actual file descriptor you can do the following:
This pipe trick should be fairly portable.