Skip to main content

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.Client) and two variants of asynchronous clients:

  • aio.AsyncClient - async interface to libmosquitto loop running in its own thread. It's a little bit faster, supposedly more reliable, but consumes a little bit more memory.
  • aio.TrueAsyncClient - manages all events in asyncio loop by utilizing mosquitto_loop_read, mosquitto_loop_write, mosquitto_loop_misc functions.

Dependencies

  • python3.8+
  • libmosquitto1

Installation

  • pip install pymosquitto

TODO

  • add v5 support

Usage

from pymosquitto import Client


def on_message(client, userdata, msg):
    print(msg)


client = Client()
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 AsyncClient


async def main():
    async with AsyncClient() 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.2 MB).

benchmark-results

Losers excluded:

benchmark-results-fast

benchmark.csv

Module;Time;RSS
pymosq;0:04.20;18144
pymosq_async;0:07.36;25604
pymosq_true_async;0:10.01;24896
paho;0:08.94;23472
gmqtt;0:04.40;24768
mqttools;0:06.20;27664
aiomqtt;0:54.12;577624
amqtt;1:04.37;745276

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymosquitto-0.3.1.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pymosquitto-0.3.1-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file pymosquitto-0.3.1.tar.gz.

File metadata

  • Download URL: pymosquitto-0.3.1.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for pymosquitto-0.3.1.tar.gz
Algorithm Hash digest
SHA256 5c13cc38c6344f8c6c66f95b5873ab94a0fbc96abdb7d476b34ad46875a8e82a
MD5 7e00e91b2c6b80c8a8c30de653d7d645
BLAKE2b-256 769c9a4ba7370378a819d3356e79f882c91144fea1dc6a75fbeeb42b0bc6dd3e

See more details on using hashes here.

File details

Details for the file pymosquitto-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: pymosquitto-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for pymosquitto-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 75aae102a5cbf266a825b3308a557f0a9fd8b4e0fe249195b346d3b4008f9ad0
MD5 a2178b6f80e2956620b1bd9e80dd45de
BLAKE2b-256 b3bd351c2c854b6503a28621d97015258d3893e2bb4eb1d6f9cc25dcfbef5e51

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page