micropython-lib icon indicating copy to clipboard operation
micropython-lib copied to clipboard

wait_msg() return op as indication if there was a message

Open meirarmon opened this issue 3 years ago • 0 comments

As indicated in this issue: https://github.com/micropython/micropython-lib/issues/328, it would be useful if wait_msg() would return a value if a message was received. A common use case using this library would be:

def callback(topic, msg):
    print(topic, msg)


async def check_messages(mqtt_client, interval):
    while True:
        mqtt_client.check_msg()
        await asyncio.sleep(interval)

In the above scenario, if more than one message was published to the topic being subscribed to, it will still get consumed every seconds. If this change is merged, we would be able to do:

def callback(topic, msg):
    print(topic, msg)


async def check_messages(mqtt_client, interval):
    while True:
        while mqtt_client.check_msg() is not None:
            continue
        await asyncio.sleep(interval)

And any waiting messages will get consumed immediately.

meirarmon avatar Sep 06 '22 18:09 meirarmon