3

Im a threading noob. I'm trying to run a function as a thread that weighs something, and another that checks that the weight is sensible -- and shuts down both threads if the weights are not. This is running on a beagleboneblack -- but that is probably not relevant. For the purposes of simplicity, I include here code that produces the same sort of undesired behaviour -- but is simplified.

from time import sleep
import threading
import Queue
import random

exitFlagMass = 0
exitFlagWD = 0
mass = None
random.seed()

class massThread (threading.Thread):
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
        #self.mass = None
    def run(self):
        global mass
        print "Starting " + self.name
        mass = massfunc(self.name)
        print "Exiting " + self.name

def massfunc(threadName):
    global exitFlagMass
    global mass
    while 1:
        sleep(2.5)
        print "exitFlagMass = "+str(exitFlagMass)
        if exitFlagMass:
            print "thread mass exiting"
            thread.exit()

        mass =  random.random()*6
        print str(mass)+" kg"

def wdfunc(threadName):
    global exitFlagWD
    global exitFlagMass
    global mass
    while 1:
        #threadLock.acquire()
        print "exitFlagWD = "+str(exitFlagWD)
        if exitFlagWD:
            print "thread wd exiting"
            thread.exit()

        if mass > 4.5:
            print "greater than"+ str(4.5)+" kg"  
            exitFlagMass = 1
            exitFlagWD = 1
        #threadLock.release()
        sleep(0.25)

class watchdogThread (threading.Thread):
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
        self.masslimit = 4.5 #max kg weight

    def run(self):
        print "Starting " + self.name
        wdfunc(self.name)
        print "Exiting " + self.name   


def main():


    # Create new threads
    weighthread = massThread(1, "weighthread-1", 1)
    wdthread = watchdogThread(2, "wdthread-2", 1)

    # Start new Threads
    wdthread.start()
    weighthread.start()


    return 0    


if __name__ == '__main__':
    main()

The issue here is that the wdthread never starts. I'm hoping there is a simple explanation. Is it because I have two threading classes? If so how do I do this with only one thread class.

Thanks for your help.

Kind regards

Matt

3
  • Global variables are a big no no, when your program gets bigger it becomes a mess. For instance if you have two variables named the same, you'll get messed up values. Also it doesn't help to read the code, if a function clearly explicits what variables it needs by requiring them as parameters then anyone can understand what it does, here's it unclear. How should you do it then? Pass the data you want to the class when creating the instance and store it as an attribute. Commented Nov 11, 2013 at 9:41
  • Works fine for me (at least until it tries to exit), both threads start and run for a bit then exit. There's a slight problem when the code exists because you didn't call thread.join() on the threads so by the time they try to call thread.exit() the global thread no longer exists. Commented Nov 11, 2013 at 9:50
  • Really? I've dropped in the joins, and it does not error, but it the wdthread is not killing the other process when the value reads over 4.5. Is it doing that for you? Maybe it is a beaglebone black implementation thing? Commented Nov 11, 2013 at 11:32

1 Answer 1

1

I think you return very early.

wdthread.join() # wait till the thread finishes
weighthread.join() # wait till the thread finishes

return 0

This would be your problem.

Sign up to request clarification or add additional context in comments.

5 Comments

The other threads should keep the process alive as they are not daemon threads.
they keep the process alive, but they don't keep the global namespace intact so the calls to thread.exit() will fail.
For that matter the call to wdfunc() might also not happen until after the global namespace has been destroyed and in that case although the thread starts it would die immediately. There should still be a traceback though.
Sorry Duncan, I'm not understanding this. I've inserted the join statements indicated by scriptmonster -- but still have the same problem with the wdfunc() not apparently doing anything. I'm thinking that what you are saying holds the key -- but I don't know how to implement a solution. THanks
Your code works for me, when I replace thread.exit() with break on both functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.