Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » multi-process message callback(Processing multiple message callbacks simultaneously)
multi-process message callback [message #1836121] Tue, 22 December 2020 02:11
Jeffrey Lavoie is currently offline Jeffrey LavoieFriend
Messages: 1
Registered: December 2020
Junior Member
Hi everyone,

So, I'm making a controller for power outlets that are controlled by i2c on a PDU.

Basically, I have one topic for each outlet for commands to be sent (published) to and another topic to be monitored (subscribed) for the current state of the outlets.

The command topic is monitored for the particular command, on; off; switch (opposite of current state); restart. When it received a command it then publishes to the state topic.

Most of this is easy to do and there's no need for multi-processing. However, when a restart command is received, a message is published to the state topic for off (whether on or off), then sleeps for a second (or whatever time is configured) and then a message is published again to turn it back on.

There is a separate function to receive the state messages that actually controls the i2c chips.

What I need, is to be able to run the functions called by message_callback_add simultaneously so that if multiple devices need to be restarted they can all turn off at practically the same time (whatever delay between your fingers moving between the buttons), sleep at the same time and then turn on without having to wait for the previous button press to be fully processed.

I tried using the thread6 library, but it seems that after enough presses it starts to slow down, like maybe memory isn't being released? I've heard this can be a problem with threading. So, I'd like to do it as multi-processing instead of multi-threading.

I'm not quote sure how to do that within the message_callback_add function.

I tried calling a function that then creates the process and calls another function

without having to put the whole code at the moment, here's the stripped down version of what I'm questioning.

def recCmdMP(client, userdata, message):
    p = multiprocessing.Process(target=recCmd, args=(client, userdata, message)
    p.start()

def recCmd(client, userdata, message):
    ( do stuff here to decode information and set State )
    Topic = "PDU/"+str(deviceID)+"/"+Bank+"/"+Outlet+"/"
    if State == '0':
        client.publish(Topic+'STATE','0',0,True)
    ( other states listed as well with similar code )
    client.unsubscribe(Topic+'COMMAND') # Don't want to think we're receiving another command when we're just clearing the retained message.
    client.publish(Topic+'COMMAND','',0,True)
    client.subscribe(Topic+'COMMAND')



and in the main code I have

client.message_callback_add("PDU/%s/+/+/COMMAND" % deviceID, recCmdMP)


I won't go into the code for the STATE topic yet as it doesn't even get that far.

What keeps happening is that when the command is received, it decodes the info, sets the state properly (verified by adding print commands in between lines of code) and then appears to run the client.publish command, but nothing happens. Running another client (MQTT Explorer) that monitors all the topics, it never actually published the message. I even set the logs to debug and checked the logs, nothing received by the server for a publish command.

So, at this point my only thought is that for some reason, even though it's not giving me any errors in the code, maybe because it's running as a separate process the function does not know what client.publish actually is?

I can give the whole code if necessary, but at this point what I'm really wondering is what might be the proper way to run a function from the message callback as separate processes? I have absolutely no prior experience with multiprocessing, so I really have no idea if I'm doing that the proper way of if it's something I'm doing wrong in the way I'm calling it from the message callback.

I appreciate any help.

[Updated on: Thu, 24 December 2020 04:14]

Report message to a moderator

Previous Topic: Paho MQTT and autorun
Next Topic:monitor-thresh
Goto Forum:
  


Current Time: Fri May 03 05:41:34 GMT 2024

Powered by FUDForum. Page generated in 0.02734 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top