Python生产者与消费者模型

1、

coding : UTF-8

import threading, time # 导入相关模块

class Message: # 数据的描述类型

def init(self): # 构造方法

self.__title = None # 初始化属性

self.__content = None # 初始化属性

def set_info(self, title, content): # 进行属性内容的设置

self.__title = title # 设置属性内容

time.sleep(1) # 进行操作的生产延迟

self.__content = content # 设置属性内容

print("【%s】title = %s、content = %s" % (threading.current_thread().name, self.__title, self.__content))

def str(self): # 获取数据将由消费者负责

time.sleep(0.8) # 消费者的延迟时间短

return "〖%s〗title = %s、content = %s" % (threading.current_thread().name, self.__title, self.__content)

def producer_handle(message): # 生产者处理函数

for num in range(50): # 生产50组数据

if num % 2 == 0 : # 交替生产

message.set_info("李兴华", "软件技术讲师")

else:

message.set_info("yootk", "www.yootk.com")

def consumer_handle(message): # 消费者处理函数

for num in range(50):

print(message) # 获取50次的数据

def main(): # 主函数

message = Message() # 公共保存的数据对象

producer_thread = threading.Thread(target=producer_handle, name="生产者线程", args=(message,))

consumer_thread = threading.Thread(target=consumer_handle, name="消费者线程", args=(message,))

producer_thread.start() # 启动线程

consumer_thread.start() # 启动线程

if name == "main": # 判断程序执行名称

main() # 调用主函数

2、

coding : UTF-8

import threading, time # 导入相关模块

class Message: # 数据的描述类型

def init(self, condition): # 构造方法

self.__title = None # 初始化属性

self.__content = None # 初始化属性

self.__condition = condition # 获取同步锁

flag = True表示可以生产,但是不能够消费

flag = False表示可以消费,但是不能够生产

self.__flag = True

def set_info(self, title, content): # 进行属性内容的设置

self.__condition.acquire() # 获取同步锁

if self.__flag == False: # 不能够继续生产了,必须进行消费处理

self.__condition.wait() # 当前的线程进入到阻塞状态

如果现在可以生产则一定会执行如下的代码操作,进行数据的设置,同时修改flag标志(此时的flag = True)

self.__title = title # 设置属性内容

time.sleep(1) # 进行操作的生产延迟

self.__content = content # 设置属性内容

print("【%s】title = %s、content = %s" % (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() # 等待生产者生产数据

如果执行了如下的代码,则意味着不进行等待(flag = False)

try:

time.sleep(0.8) # 消费者的延迟时间短

return "〖%s〗title = %s、content = %s" % (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): # 生产50组数据

if num % 2 == 0 : # 交替生产

message.set_info("李", "软件技术讲师")

else:

message.set_info("yootk", "www.com")

def consumer_handle(message): # 消费者处理函数

for num in range(50):

print(message) # 获取50次的数据

def main(): # 主函数

condition = threading.Condition() # 实例化条件锁

message = Message(condition) # 公共保存的数据对象

producer_thread = threading.Thread(target=producer_handle, name="生产者线程", args=(message,))

consumer_thread = threading.Thread(target=consumer_handle, name="消费者线程", args=(message,))

producer_thread.start() # 启动线程

consumer_thread.start() # 启动线程

if name == "main": # 判断程序执行名称

main() # 调用主函数

3、

coding : UTF-8

import threading, time, queue # 导入相关模块

class Message: # 数据的描述类型

def init(self): # 构造方法

self.__title = None # 初始化属性

self.__content = None # 初始化属性

def set_info(self, title, content): # 进行属性内容的设置

self.__title = title # 设置属性内容

time.sleep(0.1) # 进行操作的生产延迟

self.__content = content # 设置属性内容

print("【%s】title = %s、content = %s" % (threading.current_thread().name, self.__title, self.__content))

def str(self): # 获取数据将由消费者负责

time.sleep(0.8) # 消费者的延迟时间短

return "〖%s〗title = %s、content = %s" % (threading.current_thread().name, self.__title, self.__content)

def producer_handle(worker_queue): # 生产者处理函数

for num in range(50): # 生产50组数据

message = Message() # 定义生产数据的包装类型

if num % 2 == 0 : # 交替生产

message.set_info("李兴华", "软件技术讲师")

else:

message.set_info("ww", "www.com")

worker_queue.put(message) # 将生产后的数据放到队列之中

def consumer_handle(worker_queue): # 消费者处理函数

for num in range(50):

print(worker_queue.get()) # 通过队列获取数据

def main(): # 主函数

worker_queue = queue.Queue(5) # 定义一个5个大小的队列

producer_thread = threading.Thread(target=producer_handle, name="生产者线程", args=(worker_queue,))

consumer_thread = threading.Thread(target=consumer_handle, name="消费者线程", args=(worker_queue,))

producer_thread.start() # 启动线程

consumer_thread.start() # 启动线程

if name == "main": # 判断程序执行名称

main() # 调用主函数

相关推荐
C#Thread15 分钟前
C#上位机--循环语句
开发语言·c#
EterNity_TiMe_18 分钟前
【人工智能】蓝耘智算平台盛大发布DeepSeek满血版:开创AI推理体验新纪元
人工智能·python·机器学习·deepseek
diemeng111941 分钟前
2024系统编程语言风云变幻:Rust持续领跑,Zig与Ada异军突起
开发语言·前端·后端·rust
顾德拉科43 分钟前
使用pyinstaller对gradio和chromadb进行打包
python
软件黑马王子1 小时前
Unity游戏制作中的C#基础(3)加减乘除算术操作符,比较运算符,逻辑与,或运算符
开发语言·unity·c#
张太行_1 小时前
Qt Creator 设计界面后的预览方法
开发语言·qt
视觉CG1 小时前
【Viewer.js】vue3封装图片查看器
开发语言·javascript·vue.js
h^hh1 小时前
洛谷 P3405 [USACO16DEC] Cities and States S(详解)c++
开发语言·数据结构·c++·算法·哈希算法
qwy7152292581631 小时前
20-R 绘图 - 饼图
开发语言·数据库·r语言
java1234_小锋1 小时前
一周学会Flask3 Python Web开发-redirect重定向
前端·python·flask·flask3