try:
    if 'foo' not in global_dict:
        global_dict['foo'] = 1
finally:
    lock.release()

def thread_B():
    lock.acquire()
try:
    global_dict['foo'] = 2
finally:
    lock.release()
Using a global dictionary with threads in Python