paho.mqtt.python icon indicating copy to clipboard operation
paho.mqtt.python copied to clipboard

Real-time Callback Processing

Open dorintt opened this issue 7 years ago • 4 comments

Let's say I have one client publishing 10 times a second on a given topic, and another client that must process the data on that topic, but the processing takes 150 ms (more than 1/10 seconds).

Obviously, the processing client cannot keep up with the messages, so the callbacks will queue up. However, I want my client to always process the latest message, and ignore the ones that arrive while a message is still being processed (the callback didn't return yet).

What is the correct way / recommended methods to handle this problem and not have an infinitely growing callback stack?

dorintt avatar Mar 15 '19 12:03 dorintt

You can create a global variable called PROCESSING = False. And inside on message callback,

if PROCESSING == False: 
    set PROCESSING = True
    start processing data in a seperate thread and ensure that after processing data you set PROCESSING to False again
else:
    pass

I think it would be much better if you use locks instead of PROCESSING variable

tarunw07 avatar Mar 15 '19 15:03 tarunw07

Here is my recommendation: When using callbacks, do not do any processing. Just populate a queue in the on_message callback. Then in a separate thread, you can do whatever you want. It is really not a good idea to block the callback. It will start acting strangely at some point in production and cause issues.

the on_message callback:

    def on_message(self, client, userdata, msg):
        logger.info("Received message", mqtt_msg=msg)
        # only populate the mq queue object when you are not processing.
        # processing is a threading event
        # https://docs.python.org/2/library/threading.html#event-objects 
        if processing.is_set() is False:
            mq.put(msg)

Then in another thread:

    msg = mq.get(block=True)
    processing.set()
    # process the msg
    processing.clear()

Lastly, this is my personal opinion: I always thought of the github issues to either propose a new feature, ask a library specific question or report a bug.

Your question is more suited to stackoverflow since it is asking a general programming question.

This is just an opinion and I am happy to change it.

@PierreF what do you think of my stackoverflow comment?

vrst37 avatar Mar 28 '19 08:03 vrst37

Thank you for the responses. I successfully used threading locks with threads started from the callback.

It's true that this is a more general question suited for stackoverflow, but I wanted to make sure the library doesn't already have some hidden built-in functionality to handle this automatically (for cleaner application code and / or to help newbies that are not so familiar with threading subtleties).

Here is another solution using events for when there is only one callback processing function:

message_data = None
callbackEvent = threading.Event()


def callback(client, userdata, message):
    global message_data
    message_data = message.payload
    callbackEvent.set()


def main():
    while True:
        callbackEvent.wait()
        callbackEvent.clear()
        process_data(message_data)

This would have the advantage of no thread starting overhead.

dorintt avatar Apr 08 '19 14:04 dorintt

I think that this issue has been answered, so I'm going to close it. If there's still more to add, please reopen and ask/answer away!

ralight avatar Jul 22 '21 10:07 ralight