Python 互斥锁/可重入锁

Python多线程场景下,数据在不同线程下互斥访问,会涉及到同步锁

  • 互斥锁: threading.Lock()
  • 可重入锁: threading.RLock()

1.同步锁示例

1
2
3
4
5
6

    def count_increment(self):
        with self.lock:
            for i in range(5):
                SharedData._count += i
            print(f"count:{SharedData._count}")

2.SharedData其它代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14

    def __init__(self):
        # self.lock = threading.RLock()
        self.lock = threading.Lock()
        self.threads = []

    def start(self):

        for i in range(5):
            self.threads.append(threading.Thread(target=self.count_increment))
            self.threads[i].start()

        for i in range(5):
            self.threads[i].join()   

3.执行情况

1
2
3
4
5
6

count:10
count:20
count:30
count:40
count:50