Python: sharing class variables across threads
<p>I have a counter (training_queue) shared among many instances of a class. The class inherits threading.Thread, so it implements a run() method. When I call start(), I expect each thread to increment this counter, so when it reaches a limit no more threads are started. However, none of the threads modifies the variable. Here's the code:</p>
<pre><code>class Engine(threading.Thread):

    training_mutex = threading.Semaphore(MAX_TRAIN)
    training_queue = 0
    analysis_mutex = threading.Semaphore(MAX_ANALYSIS)
    analysis_queue = 0
    variable_mutex = threading.Lock()


    def __init__(self, config):
        threading.Thread.__init__(self)
        self.config = config
        self.deepnet = None
        # prevents engine from doing analysis while training
        self.analyze_lock = threading.Lock()

    def run(self):
        with self.variable_mutex:
            self.training_queue += 1
        print self.training_queue
        with self.training_mutex:
            with self.analyze_lock:
                self.deepnet = self.loadLSTM3Model()
   
</code></pre>
<p>I protect the training_queue with a Lock, so it should be thread-safe. How ever, if I print its value its always 1. How does threading affect variable scope in this case?</p>