from threading import Thread # Example code by Akshar Raaj # from http://agiliq.com/blog/2013/09/understanding-threads-in-python/ #define a global variable some_var = 0 class IncrementThread(Thread): def run(self): #we want to read a global variable #and then increment it global some_var read_value = some_var print("some_var in {} before increment is {}" .format(self.name, read_value)) some_var = read_value + 1 print("some_var in {} after increment is {}" .format(self.name, some_var)) def use_increment_thread(num_increments): threads = [] # start some threads for i in range(num_increments): t = IncrementThread() threads.append(t) t.start() # wait for all threads to end for t in threads: t.join() print("After the modifications, some_var should have become {}" .format(num_increments)) print("After the modifications, some_var is actually {}" .format(some_var)) if __name__ == '__main__': use_increment_thread(10)