How can I have paho.mqtt.subscribe.simple (Python) return None when the topic is empty/doesn't exist

1141 views
Skip to first unread message

slowb...@gmail.com

unread,
Oct 1, 2018, 12:51:35 AM10/1/18
to MQTT
I am using the following code with paho.mqtt.subscribe.simple (Python) but it just hangs when the topic doesn't exist. I need it to time out and return None or "" (empty string) when the topic does not exist. I also need it to return None or "" if the topic exists but is empty. The code is functional when the topic exists and is not empty.

How do I accomplish this?

import paho.mqtt.subscribe as subscribe

def query(topic):
  # All other variables except topic come from the global scope
 
return subscribe.simple(
    topics
=topic,
    qos
=1,
    msg_count
=1,
    retained
=True,
    hostname
=hostname,
    port
=port,
    client_id
=client_id,
    keepalive
=10,
    auth
={'username':username, 'password':password},
    tls
={'ca_certs':ca_certs})

slowb...@gmail.com

unread,
Oct 1, 2018, 1:09:10 AM10/1/18
to MQTT
Update: I looked at the source code for paho.mqtt.subscribe and on line 170 in the callback there is a client.loop_forever(). The only disconnect()s are in the _on_message_simple() internal callback on lines 57 and 62.

So it doesn't look like simple() will ever return when the topic does not exist. It will loop forever waiting for an incoming message that may never arrive.

If I want to implement this I think I will have to alter that. Probably change the loop_forever() to something like this, and add a timeout argument to the callback() function. I'll try it later as it is too late tonight to focus on it.

Your thoughts?

if timeout:
   
for i in range(timeout):
        client
.loop()
    client
.disconnect()
else:
    client
.loop_forever()

Thomas

unread,
Oct 1, 2018, 4:53:50 AM10/1/18
to MQTT
Hi - this mailing list is about the MQTT protocol. 

If you've having a problem with a particular implementation, please 
contact its support channels. 

The Paho mailing list is here: 
https://accounts.eclipse.org/mailing-list/paho-dev 

Thomas

Karl Palsson

unread,
Oct 1, 2018, 6:02:01 AM10/1/18
to mq...@googlegroups.com

You're simply misunderstanding mqtt fundamental concepts. Topics
are only defined by subscribers and publishers. There's no such
thing as subscribing to a topic that doesn't exist. You
subscribed to it, which makes it exist. Whether anyone else will
publish to it is irrelevant.

So your code isn't "hanging" it's "waiting for a message to be
published, as requested" It's potentially not unreasonable for
the "simple" apis to have timeouts, but they are the "simple"
apis. You can always just subscribe, run your network loop for
your timeout value, and then exit.

Cheers,
Karl P

slowb...@gmail.com wrote:
> Update: I looked at the source code for paho.mqtt.subscribe
> <https://github.com/eclipse/paho.mqtt.python/blob/master/src/paho/mqtt/subscribe.py>
> and on line 170 in the callback there is a
> client.loop_forever(). The only disconnect()s are in the
> _on_message_simple() internal callback on lines 57 and 62.
>
> So it doesn't look like simple() will ever return when the
> topic does not exist. It will loop forever waiting for an
> incoming message that may never arrive.
>
> If I want to implement this I think I will have to alter that.
> Probably change the loop_forever() to something like this, and
> add a timeout argument to the callback() function. I'll try it
> later as it is too late tonight to focus on it.
>
> Your thoughts?
>
> if timeout:
> for i in range(timeout):
> client.loop()
> client.disconnect()
> else:
> client.loop_forever()
>
>
>
> On Monday, October 1, 2018 at 12:51:35 AM UTC-4,
> slowb...@gmail.com wrote:
> >
> > I am using the following code with paho.mqtt.subscribe.simple
> > <https://www.eclipse.org/paho/clients/python/docs/#subscribe-unsubscribe>
signature.html

Slow Bro

unread,
Oct 1, 2018, 9:50:23 AM10/1/18
to mq...@googlegroups.com
Sorry Thomas. I got here on a link inside the Paho documentation thinking this group must cover all implementations. I will take my question there. 
--
To learn more about MQTT please visit http://mqtt.org
---
You received this message because you are subscribed to the Google Groups "MQTT" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mqtt+unsubscribe@googlegroups.com.
To post to this group, send email to mq...@googlegroups.com.
Visit this group at https://groups.google.com/group/mqtt.
For more options, visit https://groups.google.com/d/optout.

Thomas

unread,
Oct 2, 2018, 5:51:58 AM10/2/18
to MQTT
No worries... seems that I got it wrong as well, see the answer above.

Good luck with you MQTT endeavour,
Thomas
To unsubscribe from this group and stop receiving emails from it, send an email to mqtt+uns...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages