Page 1 of 1

uasyncio.udp lockup

Posted: Thu Jun 07, 2018 12:23 am
by anth
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()

Re: uasyncio.udp lockup

Posted: Sat Jun 09, 2018 1:37 pm
by Damien
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.