Synchronization mechanism in Python multithreading

with cond: is a way to synchronize threads in Python.

In multi-threaded concurrent programming, if multiple threads need to share data or resources, it is easy to cause race conditions and cause data problems. In order to avoid this situation, locks (Lock), condition variables (Condition), semaphore (Semaphore) and other mechanisms can be used to ensure data synchronization.
Among them, condition variables are a very commonly used synchronization mechanism. It allows the thread to operate only when a certain condition is met, otherwise it will wait forever.

Specifically, when a thread calls the wait() method of the condition variable, it will release the currently held lock and enter the waiting state until another thread calls the notify() or notify_all() method of the condition variable to wake it up.
Here is a simple example that demonstrates how to use condition variables to achieve thread synchronization:

import threading


data = []
cond = threading.Condition()


class ProducerThread(threading.Thread):
    def run(self):
        global data
        for i in range(10):
            with cond:
                data.append(i)
                print(f"生产者线程: {i}")
                cond.notify()  # 唤醒阻塞的消费者线程
            self._sleep()

    def _sleep(self):
        import time
        time.sleep(1)


class ConsumerThread(threading.Thread):
    def run(self):
        global data
        while True:
            with cond:
                if not data:
                    cond.wait()  # 如果data为空,则阻塞线程
                print(f"消费者线程: {data.pop(0)}")
            self._sleep()

    def _sleep(self):
        import time
        time.sleep(1)


# 启动生产者线程和消费者线程
producer = ProducerThread()
consumer = ConsumerThread()
producer.start()
consumer.start()

In this example, we define a data list, and two threads ProducerThread and ConsumerThread operate on it respectively.

When ProducerThread adds elements to the data list, it calls the notify() method of the condition variable to wake up the waiting ConsumerThread thread;

When ConsumerThread finds that the data list is empty, it will call the wait() method of the condition variable to block itself, release the lock, and wait for other threads to call the notify() method to wake it up.
It should be noted that when using condition variables, you need to acquire the lock first and release the lock at the end.

Therefore, the syntactic sugar of with cond: is often used to simplify this process.

Guess you like

Origin blog.csdn.net/qq_41579327/article/details/131661661