[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [paho-dev] Python paho.mqtt with asyncio
|
Hi,
I don't know very well asyncio or other event loop, but I don't see any
problem with your solution.
There are already two issues #147 and #72 about asynchronous loop but
currently both proposed example had drawback.
* Either using a polling (not using read-ready or write-ready event, but
a timer)
* Or using private attribute
Your solution, while a bit cumbersome to due write-ready-check, seems
the best.
Since I don't have experience with asyncio, I don't know if the library
could help using it with asyncio by exposing additional
attribute/method/callback. If you have some suggestions, I'll take them :)
Regards,
Pierre Fersing
#147: https://github.com/eclipse/paho.mqtt.python/issues/147
#72: https://github.com/eclipse/paho.mqtt.python/issues/72
Le 22/05/2017 à 11:36, Jos Vos a écrit :
> 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,
>