'python selectors based on a socket

I feel like I am missing some important information on how python selectors work. I am trying to use them with a socket. Once the socket is "registered" to the selector I start to loop and "select" on the selector (A). If that returns something the selector is "registered" with the data of the MsgEnvelope class with the code below (B). This is the point I am stuck at in the MsgEnvelope class I have a read and a write method, but I don't see that being called. (Note I have logs saying client connected but nothing after that)

    def accept_wrapper(self, tmp_sock):
        connect, address = tmp_sock.accept()
        logging.info(f"accepted connection from {address}")
        connect.setblocking(False)
        message = MsgEnvelope(self.selector, connect, address) # <<< B
        self.selector.register(connect, selectors.EVENT_READ, data=message)

    def receive(self):
        for key, mask in self.selector.select(timeout=None):   # <<< A
            if key.data is None:
                self.accept_wrapper(key.fileobj)
            else:
                yield key.data

So when the "server" registers the EVENT.READ with the message I do not understand how I can read the bytes in the MsgEnvelope class?

TLDR: What methods do I need in a class that is used as the data keyword in the selector.register method. As the documentation say

data is an opaque object.

Wat?



Solution 1:[1]

It seems I misunderstood how I should use the selector. The selector.select will yield a "key" and a "mask". The key data property is the MsgEnvelope in my case and the mask is the bits to see if it was a read or a write.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AsynchronousGillz