UDP server for new uasyncio code (V3)

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
perbu
Posts: 9
Joined: Thu Mar 26, 2020 7:34 am

UDP server for new uasyncio code (V3)

Post by perbu » Tue Apr 14, 2020 6:48 pm

Hi.

I've made a simple UDP server for the new uasyncio code. It invokes a callback when a packet arrives. It looks at the return value of the callback and sends this back the client. If the callback returns None no response is sent. It is pretty minimalist but it does what I need it do do.

One thing I was unsure of was if it is really OK to poll() the socket busy. So I added a 1ms timeout to poll(). As I understand it this is pointless on MCUs as there are no powersaving states on the MCU but if anyone would care to acknowledge that it would be great.

I use it like this:

Code: Select all

from dgram import UDPServer
import uasyncio
(..)

def cb(msg, adr):
    print('Got:', msg)
    return 'ack'.encode('ascii')

def main():
    s = dgram.UDPServer()
    l = uasyncio.get_event_loop()
    l.run_until_complete(s.serve(cb, '0.0.0.0', port))
Code is here: https://github.com/perbu/dgram
Feedback is welcome.

User avatar
tve
Posts: 216
Joined: Wed Jan 01, 2020 10:12 pm
Location: Santa Barbara, CA
Contact:

Re: UDP server for new uasyncio code (V3)

Post by tve » Tue Apr 14, 2020 10:12 pm

You didn't say which platform you're using. The esp32 definitely does some power saving.
Also, under the hood you shouldn't be polling but rather doing things like in `uasyncio.streams.py`, e.g. `yield core._io_queue.queue_read(self.s)`, etc.

perbu
Posts: 9
Joined: Thu Mar 26, 2020 7:34 am

Re: UDP server for new uasyncio code (V3)

Post by perbu » Wed Apr 15, 2020 8:53 am

Hi.

Thanks for the feedback. I'm testing this on an ESP32. I've looked at streams.py and it polls (through ipoll()). AFAIK there seems to be no interrupt-driven mechanisms to hook into in uasyncio, perhaps because the ISRs are of limited use in micropython (as opposed to Unix).

run_until_complete() uses a 1ms timeout on the FDs it is watching so my module seems to have similar characteristics.

Post Reply