Using Global variables in Threads in Python
Question: I am in a situation where i want to put a thread in a loop depending on a
variable which is being changed in one of the called function in the
thread.Here is what i want.

    
    
    error= 0
    
    while( error = 0)
        run_thread = threading.Thread(target=self.run_test,args=(some arguments))
    
    if ( error = 0)
        continue
    else:
        break
    

Now run test calls a function say A and A calls B and B calls C.

    
    
    def A()
          B()
    def B()
         c()
    
    def c()
        global error
        error = 1
    

This is what i want to do but i cant work this out. If i try to print error i
get an error in code.

Can anybody please help me on this?

i am a beginner and need to get over this


Answer: 
    error = False
    
    def A():
          B()
    
    def B():
         c()
    
    def c():
        global error
        error = True
    
    def run_test():
        while not error:
            A()
        print "Error!"
    
    import threading
    run_thread = threading.Thread(target=run_test,args=())
    run_thread.start()
    

However, its better to subclass thread and re-implement run(), and also use
exceptions:

    
    
    def A():
        raise ValueError("Bad Value")
    
    import threading
    class StoppableThread(threading.Thread):
        def __init__(self, *args, **kwargs):
            self.stop = False
    
        def run(self):
            while not self.stop:
                A() #Will raise, which will stop the thread 'exceptionally'
    
        def stop(self): #Call from main thread, thread will eventually check this value and exit 'cleanly'
            self.stop = True
    

