Help me with multithreading

I am trying to use this code Can someone who is a better programmer make this multithread for me with up to 8 threads?
I cannot get more than 1 button to work when I press one it locks the pi/cayenne until that button/script/timer is done. I’d like to use this for pump controls and have multiple running at the same time but in different variations/seconds the pumps are on for each one. but not a single script because the times needed are different depending on soil moisture contents ( a whole other project ) so for now I just need this script turned into multithread and I’m tired of racking my brain trying and not getting it to work.

import cayenne.client
import time


def on_message(message):
print ("message received: " + str(message))
if ( == 7) and (message.value == “0”):execfile(“”)
if ( == 8) and (message.value == “0”):execfile(“”)
if ( == 9) and (message.value == “0”):execfile(“”)
client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message

timestamp = 0

while True:
if (time.time() > timestamp + 10):
timestamp = time.time()
i = i+1

you can do something like:

# The callback for when a message is received from Cayenne.
def on_message(message):
    print("message received: " + str(message))
    # If there is an error processing the message return an error string, otherwise return nothing.
    if ( == 2) and (message.value == "0"):
        subprocess.Popen('python3', shell=True)
    if ( == 3) and (message.value == "0"):
        subprocess.Popen('python3', shell=True)

@adam any other way?

now im getting this but it all looks good
IndentationError: expected an indented block

That looks right to me

1 Like

finally got the indent thing figured out it was copying funny.
now I get this when I go to press the button on the dashbaord.

indent preformatted text by 4 spaces

File “”, line 15, in on_message
subprocess.Popen(‘python3’, shell=True)
NameError: global name ‘subprocess’ is not defined

add import subprocess

that worked. new problem is i have to turn the button off/on to get it run the script is there a command to make it relay back to the button or for the buttom to check which status or just auto change back to last status or something?

i did not get what you want.

so to get the button to work ( run the script ) I have to press it once for on ( run script) then a second time for off ( does nothing ) but then when i hit it a 3rd time for on it will run the script.

I tried to trigger “when press on turn button X off” but it did not always work just sometimes it would work and sometime it wouldnt trigger at all.

is there any way to add the button as a momentary button?

isnt a button suppose to work this way??

this is not the best way to trigger the button.

there is no momentary button.