在多线程下线程锁Lock()当然是少不了的,一般都是这么用的:

import threading

mutex = threading.Lock()

mutex.acquire()
# do something
mutex.release()

其实可以使用with语句:

import threading

mutex = threading.Lock()

with mutex:
    # do something

这个with语句等价于:

import threading

mutex = threading.Lock()

mutex.acquire()
try:
    # do something
finally:
    mutex.release()