def interlocked_inc(n):
    for i in range(n):
        with Foo.lock:
            Foo.instance_count += 1
threads = [threading.Thread(target=interlocked_inc, args=(100000,)) for
    thread_nr in range(100)]
for thread in threads:
    thread.start()
for thread in threads:
    thread.join()
print(Foo.instance_count)
Is modifying a class variable in python threadsafe?