A Paho MQTT client supporting asyncio loop without additional setup.
Project description
Asynchronous I/O (asyncio) Paho MQTT client
A Paho MQTT client supporting asyncio loop without additional setup. Forget about configuring the Paha network-loop. The client can almost be used as a drop-in replacement for Paho Client. The asyncio loop is automatically configured when you connect. You should use Paha connect_async or extension asyncio_connect()
to when connecting to avoid blocking.
Usage
Drop-in replacement
client = AsyncioPahoClient()
client.connect_async("mqtt.eclipseprojects.io")
# remove your current looping (loop_forever() etc)
# do mqtt stuff
client.Disconnect()
Asynchronous Context Manager
The client is an Asynchronous Context Manager and can be used with the Python with statement to atomatically disconnect and clean up.
async with AsyncioPahoClient() as client:
client.connect_async("mqtt.eclipseprojects.io")
# do mqtt stuff - client.Disconnect() is called when exiting context.
Extensions
The client has some additional async features (functions prefixed with asyncio_
).
asyncio_connect
The classic Paho connect() is blocking. Paho connect_async returns before the connect is complete. Use
asyncio_connect to wait for connect to complete without blocking. This function throws exception on connect failure. Please not that asyncio_connect()
cannot be used together with on_connect
(use asyncio_add_on_connect_listener
instead of on_connect
).
async with AsyncioPahoClient() as client:
await client.asyncio_connect("mqtt.eclipseprojects.io")
Callbacks
Paho has a lot of callbacks. Async alternatives have been added for some of them, but they are mutally exclusive (you have to pick sync or async for eatch callback type).
Classic Paho | Extension alternative |
---|---|
on_connect | asyncio_add_on_connect_listener() |
on_connect_fail | asyncio_add_on_connect_fail_listener() |
async def on_connect_async(client, userdata, message) -> None:
client.subscribe("mytopic")
async with AsyncioPahoClient() as client:
client.asyncio_add_on_connect_listener(on_connect_async)
await client.asyncio_connect("mqtt.eclipseprojects.io")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for asyncio_paho-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 76e720bd00a63f079fbfd90be1a0b41f6148cc1cc2babb6af4d92b39b987ef6e |
|
MD5 | 2d5dfa9ace12d71898d140ad28f0e4cb |
|
BLAKE2b-256 | f8065378925bb66d416392b8fc83a37734ab212955bffa86c28efbecd0368f5a |