[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[paho-dev] Python paho.mqtt with asyncio
|
Hi,
I've use the Python paho.mqtt module both with its own even loop and with
a custom select() loop, where I can simply set the mqtt client fd write bit
dependend on want_write() just before entering a new select().
Now I want to use the Python asyncio loop (Python 3.5+). I can do there
something like:
loop.add_reader(fd, mqtt_r_cb, ...)
loop.add_writer(fd, mqtt_w_cb, ...)
def mqtt_r_cb(loop, client):
client.loop_read()
def mqtt_w_cb(loop, client):
client.loop_write()
But this of course results in a busy wait/poll loop. The main issue
that I have is that I have no direct control over the moment when the
event loop is going to sleep/block, in contrary to the select() loop,
where I can set the bits just before calling select().
So this is my solution now (for simplicity I left out the parameters of
the add/remove_writer functions):
def mqtt_r_cb(loop, client):
client.loop_read()
if client.want_write():
loop.add_writer(...)
else:
loop.remove_writer(...)
def mqtt_w_cb(loop, client):
client.loop_write()
if client.want_write():
loop.add_writer(...)
else:
loop.remove_writer(...)
async def mqtt_misc_loop(loop, client):
while True:
client.loop_misc()
if client.want_write():
loop.add_writer(...)
else:
loop.remove_writer(...)
await asyncio.sleep(1)
Furthermore, after any direct call of an MQTT method, e.g. client.publish(),
I'll do the same if/else clause to set the write-ready-check as needed.
Would this be a safe way to use paho.mqtt in the asyncio context or can
you think of any problems and/or better ways to cover this problem?
Regards,
--
-- Jos Vos <jos@xxxxxx>
-- X/OS Experts in Open Systems BV | Office: +31 20 6938364
-- Amsterdam, The Netherlands | Mobile: +31 6 26216181