poll.poll return list decode

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
enzo
Posts: 40
Joined: Thu Jun 11, 2020 1:03 am

poll.poll return list decode

Post by enzo » Thu Dec 17, 2020 4:03 am

Hello, I'm trying to code a simple server with some non blocking listeners waiting for incoming socket streams.
I started with this code from RealPython and began modifying it:

Code: Select all

import socket, utime
host = "10.0.0.191"
port = 15000
from uselect import poll, select
mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mySocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
mySocket.bind((host, port))
mySocket.listen(1)
print('listening on', (host, port))
mySocket.setblocking(False)
myPoll = poll()
myPoll.register(mySocket)
def accept_wrapper(sock):
	pass
def service_connection(key, mask):
	pass
while True:
    events = myPoll.poll(5000)
    print(events)
    for key, mask in events:
        if key.data is None:
            accept_wrapper(key.fileobj)
        else:
            service_connection(key, mask)
    utime.sleep(1)        
 
I didn't modify

Code: Select all

accept_wrapper
and

Code: Select all

service_connection 
yet, so their code isn't shown here to simplify all.
I can't find anywhere details of the string returning from poll.poll().
In microPython docs 1.13 they say it should be a tuple but nothing more specific: https://docs.micropython.org/en/latest/ ... elect.html
The code above treats it as if it were a dict as you can see in the while loop and it returns me an error because...
... when I print it from the above code I get a list:

Code: Select all

[(<socket state=1 timeout=0 incoming=0 off=0>, 1)]
is one example of my returns.
I really can't get out of this after many tries. Please can anyone help me? Thanks!

larsks
Posts: 22
Joined: Mon Feb 13, 2017 5:27 pm

Re: poll.poll return list decode

Post by larsks » Fri Dec 18, 2020 6:23 pm

I can't find anywhere details of the string returning from poll.poll().
The `poll` method doesn't return a string. The documentation (https://docs.micropython.org/en/latest/ ... .poll.poll) says that it returns:
[a] list of (obj, event, …) tuples. There may be other elements in tuple, depending on a platform and version, so don’t assume that its size is 2. The event element specifies which events happened with a stream and is a combination of uselect.POLL* constants described above. Note that flags uselect.POLLHUP and uselect.POLLERR can be returned at any time (even if were not asked for), and must be acted on accordingly (the corresponding stream unregistered from poll and likely closed), because otherwise all further invocations of poll() may return immediately with these flags set for this stream again.

In case of timeout, an empty list is returned.
The object is whatever file-like object you registered with the polling object.

enzo
Posts: 40
Joined: Thu Jun 11, 2020 1:03 am

Re: poll.poll return list decode

Post by enzo » Sat Dec 19, 2020 7:02 pm

Thanks Larsks,
The `poll` method doesn't return a string. The documentation (https://docs.micropython.org/en/latest/ ... .poll.poll) says that it returns:
Sorry I meant a list!
[a] list of (obj, event, …) tuples. There may be other elements in tuple, depending on a platform and version, so don’t assume that its size is 2. The event element specifies which events happened with a stream and is a combination of uselect.POLL* constants described above. Note that flags uselect.POLLHUP and uselect.POLLERR can be returned at any time (even if were not asked for), and must be acted on accordingly (the corresponding stream unregistered from poll and likely closed), because otherwise all further invocations of poll() may return immediately with these flags set for this stream again.
In case of timeout, an empty list is returned.
The object is whatever file-like object you registered with the polling object.
The code I posted returns lists similar to this one:

Code: Select all

[(<socket state=1 timeout=0 incoming=0 off=0>, 4)]
when it receives a string I send from a client but I don't know how to get the data because the example I got is for Python and it acts based on a dict received as an event and not this kind of list.
The doc you quoted tells
The event element specifies which events happened with a stream and is a combination of uselect.POLL* constants described above
but I can't find these constants anywhere..
These are the 2 orginal Python functions which I'm not able to code in microPython because I can't understand the list elements meaning:

Code: Select all

def accept_wrapper(sock):
    conn, addr = sock.accept()  # Should be ready to read
    print('accepted connection from', addr)
    conn.setblocking(False)
    data = types.SimpleNamespace(addr=addr, inb=b'', outb=b'')
    events = selectors.EVENT_READ | selectors.EVENT_WRITE
    myPoll.register(conn, events, data=data)
    
def service_connection(key, mask):
    sock = key.fileobj
    data = key.data
    if mask & selectors.EVENT_READ:
        recv_data = sock.recv(1024)  # Should be ready to read
        if recv_data:
            data.outb += recv_data
        else:
            print('closing connection to', data.addr)
            sel.unregister(sock)
            sock.close()
    if mask & selectors.EVENT_WRITE:
        if data.outb:
            print('echoing', repr(data.outb), 'to', data.addr)
            sent = sock.send(data.outb)  # Should be ready to write
            data.outb = data.outb[sent:]
Do you khere can I find a list of all returning list elements and the meaning of each one?
Some code snippets to sample from would be great, too!

larsks
Posts: 22
Joined: Mon Feb 13, 2017 5:27 pm

Re: poll.poll return list decode

Post by larsks » Sun Dec 20, 2020 3:46 am

but I can't find these constants anywhere..
The constants are defined in the `select` module, and are described briefly in that same document (https://docs.micropython.org/en/latest/ ... l.register). They mean the same thing as the corresponding values in regular Python; see https://docs.python.org/3/library/selec ... l.register.
These are the 2 original Python functions which I'm not able to code in microPython because I can't understand the list elements meaning:
I don't see either of those functions calling the `.poll()` method. It looks as if they are using the `selectors` interface (https://docs.python.org/3/library/selectors.html), which isn't implemented in MicroPython.

enzo
Posts: 40
Joined: Thu Jun 11, 2020 1:03 am

Re: poll.poll return list decode

Post by enzo » Sun Dec 20, 2020 6:09 pm

larsks wrote:
Sun Dec 20, 2020 3:46 am
but I can't find these constants anywhere..
The constants are defined in the `select` module, and are described briefly in that same document (https://docs.micropython.org/en/latest/ ... l.register). They mean the same thing as the corresponding values in regular Python; see https://docs.python.org/3/library/selec ... l.register.

These don't help me in understanding what I'm reading in the list returning from poll.poll().
What's the meaning of various "socket state = xx", "timeout=xx"? I thought "incoming=xx" were the bytes received but it seems not the case.
I don't understand how to proceed for reading what was sent in each listening thread and didn't find anything to help me. I know the original code is Python but if threads, poll etc are ported to microPython there must be a way to catch the streams they get: what's the way?
These are the 2 original Python functions which I'm not able to code in microPython because I can't understand the list elements meaning:
I don't see either of those functions calling the `.poll()` method. It looks as if they are using the `selectors` interface (https://docs.python.org/3/library/selectors.html), which isn't implemented in MicroPython.
As above: how can I catch what has been sent? I know 'selector' interface is not implemented in microPython but what are the right instructions to use?
I did many tries but I couldn't understand how reading the sent payload and cycle through the events queue.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: poll.poll return list decode

Post by pythoncoder » Mon Dec 21, 2020 8:02 am

@enzo here is an example of the use of the poll mechanism from the MicroPython source.
Peter Hinch
Index to my micropython libraries.

enzo
Posts: 40
Joined: Thu Jun 11, 2020 1:03 am

Re: poll.poll return list decode

Post by enzo » Mon Dec 21, 2020 2:13 pm

pythoncoder wrote:
Mon Dec 21, 2020 8:02 am
@enzo here is an example of the use of the poll mechanism from the MicroPython source.
Thanks a lot Peter, that is what I was searching for!
Not easy, I see. I need to study it carefully and hope to work it out!
It seems strange to me there's no exhaustive official microPython documentation for such a key topic...

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: poll.poll return list decode

Post by pythoncoder » Mon Dec 21, 2020 6:41 pm

I think @Damien is developing improvements to the poll mechanism.
Peter Hinch
Index to my micropython libraries.

enzo
Posts: 40
Joined: Thu Jun 11, 2020 1:03 am

Re: poll.poll return list decode

Post by enzo » Tue Dec 22, 2020 12:31 am

pythoncoder wrote:
Mon Dec 21, 2020 6:41 pm
I think @Damien is developing improvements to the poll mechanism.
I'll check for this.
Thanks again.

Post Reply