uasyncio.udp lockup

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
anth
Posts: 4
Joined: Thu Apr 26, 2018 1:14 am

uasyncio.udp lockup

Post by anth » Thu Jun 07, 2018 12:23 am

I'm trying to get udp packets asynchronously, but usayncio locks up and a TimeoutError is not thrown when timeout expires.

Here is the example code with a 1 second timeout that just sits locked up:

Code: Select all

#uasyncio.udp lockup example

import uasyncio as asyncio

import network
import socket

import uasyncio
import uasyncio.udp
import usocket as socket

print('Started')

MCAST_GRP = '224.1.1.1'
MCAST_PORT = 30718

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', MCAST_PORT))  # use MCAST_GRP instead of '' to listen only
                             # to MCAST_GRP, not all groups on MCAST_PORT

loop = asyncio.get_event_loop()

async def lockupdemo():
    while True:
        resp,addr = yield from uasyncio.wait_for(uasyncio.udp.recvfrom(sock, 4), 1)
        print('done')

loop.create_task(lockupdemo()) # Schedule ASAP

loop.run_forever()

Damien
Site Admin
Posts: 560
Joined: Mon Dec 09, 2013 5:02 pm

Re: uasyncio.udp lockup

Post by Damien » Sat Jun 09, 2018 1:37 pm

You'll probably need to set the UDP socket as non-blocking after you create it, using s.setblocking(False).

For convenience you can use the uasyncio.udp.socket() function which creates a DGRAM socket and sets it to non-blocking for you.

Post Reply