Python线程安全
try-finally 模式
import threading
lock = threading.Lock()
lock.acquire()
try:
#do something
finally:
lock.relase()
with 模式
import threading
lock = threading.Lock()
with lock:
#do something
import threading
lock = threading.Lock()
lock.acquire()
try:
#do something
finally:
lock.relase()
import threading
lock = threading.Lock()
with lock:
#do something