2. threading.Lock() 的必要性
3.观察block
4.threading.RLock() 的应用场景
1.参考
Thread Synchronization Mechanisms in Python
count += 1 不是原子操作,三步操作可能被中断,通过lock将三步操作“封装”为一步操作,要么执行,要么不执行。
counter = 0 def process_item(item): global counter ... do something with item ... counter += 1 # The reason for this is that the increment operation is actually executed in three steps; #first, the interpreter fetches the current value of the counter, # then it calculates the new value, # and finally, it writes the new value back to the variable.