Closed wasdee closed 2 years ago
In your first example you aren't using an event loop, so you aren't responding to the requirements of the socket.
Please use one of:
mqtt_client = Client()
mqtt_client.will_set( 'topicX', 'offline', 1, True)
mqtt_client.connect(keepalive=60) # takes 3 loop max
mqtt_client.publish('topicX', 'online', qos=1, retain=True)
mqtt_client.loop_forever()
# This part is "never" reached
Or:
mqtt_client = Client()
mqtt_client.will_set( 'topicX', 'offline', 1, True)
mqtt_client.connect(keepalive=60) # takes 3 loop max
mqtt_client.publish('topicX', 'online', qos=1, retain=True)
mqtt_client.loop_start()
while True:
time.sleep(20)
# Do other things
I have a real-time application (msec precision). I found that it is better to manage when the loop
will happen since Python has GIL. unless other thread is IO-blocking, the callback or publish is not guaranteed.
@ralight do you have recommendation for this?
I use asyncio
in my code. but i don't put it here since I don't think it is applicable at first.
Basically, if I want to use loop*(). you suggest to follow this example, right?
In that case I would take a look at this example: https://github.com/eclipse/paho.mqtt.python/blob/master/examples/loop_asyncio.py
Thank you for your advice.
I would use https://github.com/sbtinstruments/asyncio-mqtt . It looks zen to me. I tested, it works just fine.
as you might know that, the lib recommend us to use
loop_*()
instead ofloop()
I try to incorporate
loop_*()
as much as possible in by code. However,Reproduce
after 1-2 min, the
will
will send out, the client could not keep alive.Workaround
this will work as expect.