sbtinstruments / aiomqtt

The idiomatic asyncio MQTT client, wrapped around paho-mqtt
https://sbtinstruments.github.io/aiomqtt
BSD 3-Clause "New" or "Revised" License
392 stars 71 forks source link

Exception occurred on ver 2.0.0 #266

Closed musimasami closed 5 months ago

musimasami commented 5 months ago

I change ver 1.2.1 to 2.0.0

On Ver 2.0.0, I get Error message as below.

Error message Exception has occurred: TypeError 'async_generator' object is not callable File "C:\Users\masami\pytest\xxtemp_mqtt.py", line 29, in main async with mq_client.messages() as messages: ^^^^^^^^^^^^^^^^^^^^ File "C:\Users\masami\pytest\xxtemp_mqtt.py", line 95, in asyncio.run(main()) TypeError: 'async_generator' object is not callable

Test code

import os import sys import asyncio import aiomqtt

temp_topic = 'Myhome/temperture'

async def main(): async with aiomqtt.Client(hostname="xxxx.local",username="xxxxx",password="xxxxx",port=1883) as mq_client: await mq_client.subscribe(temp_topic) async with mq_client.messages() as messages:

await mq_client.subscribe(temp_topic)

        async for message in messages:
                print(message.payload)

if sys.platform.lower() == "win32" or os.name.lower() == "nt": from asyncio import set_event_loop_policy, WindowsSelectorEventLoopPolicy set_event_loop_policy(WindowsSelectorEventLoopPolicy())

asyncio.run(main())

comment I changed hosutoname username password

OS Windows 11

Python Python 3.12.1 (tags/v3.12.1:2305ca5, Dec 7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)] on win32

musimasami commented 5 months ago

OK on Mac os aiomqtt Ver 2.0.0 macOS Sonoma ver 14.2.1 PC MacBook Air Python Python 3.11.0 (v3.11.0:deaf509e8f, Oct 24 2022, 14:43:23) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin

Code import aiomqtt import asyncio

temp_topic = 'Myhome/temperture'

async def main(): async with aiomqtt.Client(hostname="xxxxx.local",username="xxxxxx",password="xxxxx",port=1883) as client: await client.subscribe(temp_topic) async for message in client.messages: print(message.payload)

asyncio.run(main())

musimasami commented 5 months ago

OK on Raspberry pi aiomqtt Ver 2.0.0 Raspberry pi Linux musiUSB 6.1.21-v8+ #1642 SMP PREEMPT Mon Apr 3 17:24:16 BST 2023 aarch64 GNU/Linux python Python 3.9.2 (default, Feb 28 2021, 17:03:44) [GCC 10.2.1 20210110] on linux

Code same as mac

JonathanPlasse commented 5 months ago

The following code is incorrect.

async with mq_client.messages() as messages:

It should be.

async with mq_client.messages as messages:
musimasami commented 5 months ago

I test

async with mq_client.messages as messages: OK Close I tested this code on 1.2.1. I get error. What is the difference between ver. 1.2.1 and ver.2.0.0 ? I think it need some explanation on the documentation.

empicano commented 5 months ago

Hi there! Thanks for your detailed background infos on what system and versions you're using 👍

Did you already find the migration guide in our documentation? It also contains a minimal working example of how to receive messages. To migrate, you would rewrite this:

import asyncio
import aiomqtt

async def main():
    async with aiomqtt.Client("test.mosquitto.org") as client:
        await client.subscribe("example")
        async with client.messages() as messages:
            async for message in messages:
                print(message.payload)

asyncio.run(main())

like so:

import asyncio
import aiomqtt

async def main():
    async with aiomqtt.Client("test.mosquitto.org") as client:
        await client.subscribe("example")
        async for message in client.messages:
            print(message.payload)

asyncio.run(main())

So you basically get rid of the async with client.messages() as messages line.

Please let me know if you have ideas of how to improve the migration guide 😊

musimasami commented 5 months ago

I wanted a description after "suscribe" sample code that it has changed from the previous version.