A lightweight Python MQTT client implemented as a thin wrapper around libmosquitto
Project description
PyMosquitto
A lightweight Python MQTT client implemented as a thin wrapper around libmosquitto.
It provides an efficient synchronous client (client.Mosquitto) and two variants of asynchronous clients:
aio.AsyncMosquitto- async interface to libmosquitto loop running in its own thread. It's faster, but consumes a little bit more memory.aio.TrueAsyncMosquitto- manages all events in asyncio loop by utilizingmosquitto_loop_{read,write,misc}functions.
Dependencies
- python3.8+
- libmosquitto1
Installation
- pip install pymosquitto
Usage
from pymosquitto import Mosquitto
def on_message(client, userdata, msg):
print(msg)
client = Mosquitto()
client.on_connect = lambda *_: client.subscribe("#", 1)
client.on_message = on_message
client.connect_async("localhost", 1883)
client.loop_forever()
Async client example:
import asyncio
from pymosquitto.aio import AsyncMosquitto
async def main():
async with AsyncMosquitto() as client:
await client.connect("localhost", 1883)
await client.subscribe("#", 1)
async for msg in client.read_messages():
print(msg)
asyncio.run(main())
Check out more examples in tests directory.
Benchmarks
Receiving one million messages with QoS 0.
*The memory plots exclude the Python interpreter overhead (~10.3 MB).
Losers excluded:
benchmark.csv
Module;Time;RSS
pymosq;0:04.84;18940
pymosq_async;0:08.06;25560
pymosq_true_async;0:10.78;25092
paho;0:09.07;23620
gmqtt;0:04.63;24740
mqttools;0:06.57;28068
aiomqtt;0:56.60;578380
amqtt;1:02.72;757084
License
MIT
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pymosquitto-0.4.1.tar.gz.
File metadata
- Download URL: pymosquitto-0.4.1.tar.gz
- Upload date:
- Size: 15.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b22fce9a712ac08ef75e4f46d08fe6a62559503df3b2bc0637290e1e07b300a1
|
|
| MD5 |
465fd563236e6a7f21819c090ef29d42
|
|
| BLAKE2b-256 |
f2a087711c85aa3e8a13cd38c02be9cfaf3ae9655583a200208eff8aa517d3af
|
File details
Details for the file pymosquitto-0.4.1-py3-none-any.whl.
File metadata
- Download URL: pymosquitto-0.4.1-py3-none-any.whl
- Upload date:
- Size: 14.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
82de67039769e61c9153d069669e547274226ecb2d08820956f2a3e3533b0cc7
|
|
| MD5 |
1a083afbf08ee17090499347e474df82
|
|
| BLAKE2b-256 |
eb68dd5d6e07ba62ad31d424969acff9cca6be148664a5517d3b5e0a122e4714
|