Code Monkey home page Code Monkey logo

Comments (5)

LeonFon avatar LeonFon commented on June 14, 2024 1

Yes, it is not an issue with the asyncio-mqtt, just in the example code.
Anyway it's handling the exception with your changes :)
So i think you should add that change to README.md

from aiomqtt.

frederikaalund avatar frederikaalund commented on June 14, 2024 1

Anyway it's handling the exception with your changes :)

That's great to hear. I consider this issue solved then. :)

So i think you should add that change to README.md

Already done!

from aiomqtt.

frederikaalund avatar frederikaalund commented on June 14, 2024

Thank you for reporting this issue. Let me have a look. :)

You're right, Python's bytes.decode function assumes that the binary represents an UTF8-encoded string per default. If the message.payload (bytes) object does not follow this assumption, decode will raise an exception.

Whether the MQTT message payload is UTF8-encoded text, audio data, video frames, or even empty is completely up to the application (i.e., your usage of MQTT). The asyncio-mqtt does not impose any restrictions here.

In the example code, I have assumed that the message payload is UTF8-encoded text. This was done to make the example code a simple starting point for many applications. In your case, it seems that this assumption led to headaches. Sorry about that.

I can see two options here:

  • Add a comment to the example code about the UTF8-encoded text assumption.
  • Change the example code to simply display the raw binary data (without the bytes.decode call). The output will be more messy like this but at least it won't make assumptions about the payload.

What do you think? Do you have a third option in mind?

from aiomqtt.

LeonFon avatar LeonFon commented on June 14, 2024

Thank you for your response.

My point was more about the exception handling.
If any exception will occur in this block of code the code will just hang up.

Regarding the decoding, I think that adding a comment will be enough :)

Right now i implemented this in other way:

async def start_mqtt_client():
    async with Client(
            hostname=config.mqtt.server) as client:

        tasks = []
        tasks.append(asyncio.create_task(handle_bcg(
            client.filtered_messages("/filter1"))))
        tasks.append(asyncio.create_task(handle_sts(
            client.filtered_messages("/filter2"))))

        await client.subscribe("/topic1")
        await client.subscribe("/topic2")
        await asyncio.gather(*tasks)

from aiomqtt.

frederikaalund avatar frederikaalund commented on June 14, 2024

Thanks for the follow-up with additional details.

My point was more about the exception handling.
If any exception will occur in this block of code the code will just hang up.

Ah, I see. I could reproduce this behaviour in the advanced example from README.md. I assume that your code is closely based on the advanced example.

I think that is not an issue with asyncio-mqtt itself but with the example code. Specifically, I think that I made a mistake in the cancel_tasks helper function.

Can you please try to apply the following change:

async def cancel_tasks(tasks):
    for task in tasks:
        if task.done():  # Added
            continue     # Added
        task.cancel()
        try:
            await task
        except asyncio.CancelledError:
            pass

Again thanks for reporting this issue. :)

from aiomqtt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.