Yes, you have several options. The more modern way would be to use micropython asyncio via the StreamReader class (assuming you are using ESP32 modules - see the docs). If you are not ready for asyncio, you can set the timeout for recv() to 0 to use non-blocking reads:
Code: Select all
e.config(timeout=0) # To set the default timeout for recv()/irecv() (ESP32 only)
Code: Select all
host, msg = e.irecv(0)
You may also prefer to just set the timeout to a smaller number (eg. 500 milliseconds) rather than 0.
Regarding callbacks, micropython does provide a mechanism for scheduling callbacks (and early versions of this driver used that mechanism), however, it is designed for lower-latency servicing of not-too frequent interrupts and the like. The mechanism is easily overflowed by high throughput or rapid bursts of ESPNow traffic (eg. many clients responding to an espnow broadcast) and especially if you have lots of callbacks from multiple sources (eg. hardware interrupts, bluetooth, espnow, etc). So, the current driver instead uses an internal buffer queue and a more traditional udp-like I/O model and is much, much more reliable and performant on a busy module.