Python producer and consumer condition synchronous processing and queue processing

Use condition to realize the synchronization processing of waiting for wake-up operation and production and consumption;

import threading,time,sched

class Message:
    def __init__(self,condition):
        self._title = None;
        self._content = None;
        self._flag = True;
        self._condition = condition;

    def set_info(self,title,content):
        self._condition.acquire();
        if self._flag == False:
            self._condition.wait();
        self._title = title;
        time.sleep(1);
        self._content = content;
        print("{}-title = {},content = {}".format(threading.current_thread().name,self._title,self._content));
        self._flag = False;
        self._condition.notify();
        self._condition.release();

    def __str__(self):
        self._condition.acquire();
        if self._flag == True:
            self._condition.wait();
        try:
            time.sleep(0.8);
            return "{}-title = {},content = {}".format(threading.current_thread().name,self._title,self._content);
        finally:
            self._flag = True;
            self._condition.notify();
            self._condition.release();

# 生产者处理函数
def producer_handle(message):
    for num in range(50):
        if num % 2 == 0:
            message.set_info("奥特曼","奥特曼打怪兽");
        else:
            message.set_info("黑猫警长","黑猫警长抓老鼠");

# 消费者处理函数
def consumer_handle(message):
    for num in range(50):
        print(message);

def main():
    # 实例化条件锁
    condition = threading.Condition();
    # 公共保存的数据对象
    message = Message(condition);
    prodecer_thread = threading.Thread(target=producer_handle,name = "生产者线程",args=(message,));
    consumer_thread = threading.Thread(target=consumer_handle,name = "消费者线程",args=(message,));
    prodecer_thread.start();
    consumer_thread.start();

if __name__ == '__main__':
    main();

There will be a problem with using this. If you produce one and consume one, then if you do not consume it, you will not continue to produce. This will cause waste of resources. If you have used mq, this is well understood. Use queues to solve this problem. Question;
Python provides a queue module to implement
1.queue.Queue first-in first-out synchronous queue
2.queue.LifoQueue last-in first-out synchronous queue
3.queue.PriorityQueue priority queue

There will be no problems with performance at this time, and there is no need to use a synchronization mechanism;

import threading,time,queue

class Message:
    def __init__(self):
        self._title = None;
        self._content = None;

    def set_info(self,title,content):
        self._title = title;
        self._content = content;
        time.sleep(0.1);
        print("{}-title = {},content = {}".format(threading.current_thread().name,self._title,self._content));

    def __str__(self):
        time.sleep(0.8);
        return "{}-title = {},content = {}".format(threading.current_thread().name,self._title,self._content);

# 生产者处理函数
def producer_handle(work_queue):
    for num in range(50):
        message = Message();
        if num % 2 == 0:
            message.set_info("奥特曼","奥特曼打怪兽:{}".format(num));
        else:
            message.set_info("黑猫警长","黑猫警长抓老鼠:{}".format(num));
        work_queue.put(message);
# 消费者处理函数
def consumer_handle(work_queue):
    for num in range(50):
        print(work_queue.get());

def main():

    # 创建5个队列
    work_queue = queue.Queue(5);
    prodecer_thread = threading.Thread(target=producer_handle,name = "生产者线程",args=(work_queue,));
    consumer_thread = threading.Thread(target=consumer_handle,name = "消费者线程",args=(work_queue,));
    prodecer_thread.start();
    consumer_thread.start();

if __name__ == '__main__':
    main();

Guess you like

Origin blog.csdn.net/weixin_44887276/article/details/114880577