toreamun / asyncio-paho Goto Github PK
View Code? Open in Web Editor NEWPaho MQTT client supporting asyncio loop without additional setup.
License: MIT License
Paho MQTT client supporting asyncio loop without additional setup.
License: MIT License
Hi,
Thanks for the awesome library! I'm trying to do assert that on_message was called on publish.
from unittest.mock import create_autospec
import pytest
from asyncio_paho import AsyncioPahoClient
async def on_connect_async(client, _1, _2, _3) -> None:
client.subscribe("mytopic")
async def on_message(_, _1, msg):
print(f"Received from {msg.topic}: {str(msg.payload)}")
mock_on_message = create_autospec(on_message)
@pytest.mark.asyncio
async def test_pubsub():
async with AsyncioPahoClient(client_id="paho 🐝") as client:
client.asyncio_listeners.add_on_connect(on_connect_async)
await client.asyncio_connect("localhost")
client.asyncio_listeners.add_on_message(mock_on_message)
client.publish("mytopic", "foo")
mock_on_message.assert_called_once()
However, I'm getting AssertionError: Expected 'mock' to have been called once. Called 0 times.
Thanks for taking a look :)
Hi,
maybe not direct related to your code but untill now I am not able to install asyncio-paho. I updated to python 3.10.
pi@raspberrypi-iob:~ $ sudo pip3 install asyncio-paho Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Collecting asyncio-paho Could not find a version that satisfies the requirement asyncio-paho (from versions: ) No matching distribution found for asyncio-paho pi@raspberrypi-iob:~ $ ^C pi@raspberrypi-iob:~ $ pip install asyncio-paho Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Collecting asyncio-paho Could not find a version that satisfies the requirement asyncio-paho (from versions: ) No matching distribution found for asyncio-paho pi@raspberrypi-iob:~ $ python --version Python 2.7.16 pi@raspberrypi-iob:~ $ python3. python3.10 python3.10-config python3.7 python3.7-config python3.7m python3.7m-config pi@raspberrypi-iob:~ $ python3.
perhaps you have an idea what is wrong or what must be done. pls beware of that I am a linux newbie :-)
thx in advance
thomas
Noyhing changed.....
Возникло исключение: NotImplementedError
exception: no description
File "D:\Projects_TakeYours\Server\src\server.py", line 77, in main
await client.asyncio_connect(MQTT_HOST, MQTT_PORT, 60)
During handling of the above exception, another exception occurred:
File "D:\Projects_TakeYours\Server\src\server.py", line 73, in main
async with AsyncioPahoClient() as client:
File "D:\Projects_TakeYours\Server\src\server.py", line 82, in
asyncio.run(main())
I found this repo after getting frustrated with gmqtt.
I really appreciate the type hints and wanting to be like paho client as much as possible but found the approach you took lead to some undesirable traits like 3 connect methods and the access of protected members.
Would you be interested in altering this repo to use a facade instead (I would be willing to contribute / write it with you)?
We no longer have to contend with:
- self.connect(...)
- self.connect_async(...)
- self.asyncio_connect(...)
and can instead offer only:
- def connect(...) -> None
- async def wait_connected() -> None
The documentation can be simplified to not require parts like:
The classic Paho connect() is blocking. Paho connect_async() is not blocking, but returns before the connect is complete. Use asyncio_connect() to wait for connect to complete without blocking. This function also throws exception on connect failure. Please note that asyncio_connect() cannot be used together with on_connect /on_connect_fail (use asyncio_add_on_connect_listener and asyncio_add_on_connect_fail_listener instead of on_connect and on_connect_fail).
We could add in missing type hints (paho is missing a fair amount).
To help explain further here is a bit of code:
import logging
from paho.mqtt.client import MQTT_CLEAN_START_FIRST_ONLY, Client
__all__ = ["AsyncClient"]
class _SyncClient(Client):
"""Exposes some protected values for AsyncClient
This will help us avoid lines like:
- self._asyncio_listeners._on_connect_forwarder, # pylint: disable=protected-access
- self._asyncio_listeners._on_connect_fail_forwarder, # pylint: disable=protected-access
"""
pass
class AsyncClient:
"""Facade around Paho Sync Client
We pass through only the properties and methods that make sense and fix
/ add in type hints where they didn't exist before.
"""
def __init__(self) -> None:
self._client = _SyncClient()
# we can add in missing type hints at the same time
def enable_logger(self, logger: logging.Logger = None) -> None:
self._client.enable_logger(logger)
def disable_logger(self) -> None:
self._client.disable_logger()
# more missing type hints
def username_pw_set(self, username: str, password: str = None) -> None:
self._client.username_pw_set(username, password)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.