Great question — answering as the UphillTech team:
Run the client on one event loop that lives in its own thread, connect once, and send publishes to it from other threads with run_coroutine_threadsafe import asyncio, threading from amqtt.client import MQTTClient from amqtt.mqtt.constants import QOS_1 class MQTTPublisher: def __init__(self, url="mqtt://localhost:1883"): self.loop = asyncio.new_event_loop() threading.Thread(target=self.loop.run_forever, daemon=True).start() self.client = self._run(self._connect(url)) async def _connect(self, url): client = MQTTClient() await client.connect(url) return client def _run(self, coro): return asyncio.run_coroutine_threadsafe(coro, self.loop).result() def publish(self, topic, payload: bytes,...
Full answer by Emmanuel Obiechina Ikenna: https://stackoverflow.stackexchange.com/a/80014716 (CC BY-SA 4.0).
Need it done for your business? UphillTech.