queue模块:消息安全地在多线程间交换,实现了多生产者、多消费者队列
看源码 queue 模块包含四个实用的类:
一、三种队列:
1、Queue()、SimpleQueue():先进先出队列
2025年05月26日
queue模块:消息安全地在多线程间交换,实现了多生产者、多消费者队列
看源码 queue 模块包含四个实用的类:
1、Queue()、SimpleQueue():先进先出队列
2025年05月26日
import heapq
def simulate_message_queue():
# 读取输入
message_line = input().strip().split()
consumer_line = input().strip().split()
# 解析发布者的消息
messages = []
for i in range(0, len(message_line), 2):
time = int(message_line[i])
content = int(message_line[i + 1])
messages.append((time, content))
# 解析消费者的订阅和取消订阅
consumers = []
for i in range(0, len(consumer_line), 2):
subscribe_time = int(consumer_line[i])
unsubscribe_time = int(consumer_line[i + 1])
consumers.append((subscribe_time, unsubscribe_time, i // 2)) # 消费者编号为i//2
# 收集所有事件
events = []
for time, content in messages:
events.append((time, 'message', content))
for sub_time, unsub_time, consumer_id in consumers:
events.append((sub_time, 'subscribe', consumer_id))
events.append((unsub_time, 'unsubscribe', consumer_id))
# 按时间排序事件,同一时刻的事件按订阅、取消订阅、消息的顺序处理
events.sort(key=lambda x: (x[0], 0 if x[1] == 'subscribe' else (1 if x[1] == 'unsubscribe' else 2)))
# 当前订阅的消费者,使用最小堆模拟最大优先级队列(优先级是负数)
active_consumers = []
consumer_messages = [[] for _ in range(len(consumers))]
for event in events:
time, event_type, data = event
if event_type == 'subscribe':
consumer_id = data
# 使用负数模拟最大堆,因为消费者按优先级升序排列
heapq.heappush(active_consumers, (-consumer_id, consumer_id))
elif event_type == 'unsubscribe':
consumer_id = data
# 需要从堆中移除该消费者,这里简化处理,假设取消订阅时消费者一定在堆顶
if active_consumers and active_consumers[0][1] == consumer_id:
heapq.heappop(active_consumers)
else:
# 如果消费者不在堆顶,需要遍历整个堆来移除
temp = []
found = False
while active_consumers:
item = heapq.heappop(active_consumers)
if item[1] == consumer_id:
found = True
break
temp.append(item)
if not found:
for item in temp:
heapq.heappush(active_consumers, item)
elif event_type == 'message':
if active_consumers:
# 获取优先级最高的消费者
_, consumer_id = active_consumers[0]
consumer_messages[consumer_id].append(data)
# 输出结果
for messages in consumer_messages:
if messages:
print(' '.join(map(str, messages)))
else:
print(-1)
simulate_message_queue()
2025年05月26日
进程彼此之间互相隔离,要实现进程间通信(IPC),multiprocessing模块支持两种形式:队列和管道(不推荐使用),这两种方式都是使用消息传递的
2025年05月26日
上篇文章介绍了 Python 中的多线程。今天来介绍下编程中常会用到的一个数据结构 - 队列。
不知道大家是否还记得什么是数据结构呢?在很早很早以前,Python小课堂的初期,讲了许多 Python 原生的数据结构。比如 list、tuple、dict 等。。。
既然叫数据结构,实际上就是为了给计算机存储数据用的一种结构体。不同的数据结构都有其不同的特点。那今天就来简单地聊聊队列!
2025年05月26日
2025年05月26日
webpack+react demo下载
项目的启动,参考
1.工程下载下来之后,在src目录下新建目录“table”,新建app.js,内容如下。
import React from 'react'; import ReactDOM from 'react-dom'; import ExampleTable from './ExampleTable'; import 'antd/dist/antd.css'; ReactDOM.render ( <ExampleTable />, document.body );
2025年05月26日
e公司讯,从中科院背景企业中科闻歌获悉,旗下雅意大模型自6月份发布以来,已在多家知名机构与企业部署。当前,雅意大模型支持MaaS服务、一体机、全栈式出售三种服务形式,面向行业用户,开启预售。其中,雅意大模型MaaS服务模式拥有多种参数规模,提供WEBChat和API两种服务方式,并根据tokens(模型所处理的文本词块数量)使用情况收费。而雅意大模型一体机服务模式,拥有复杂信息抽取、多模态内容生成、深度语义理解、领域知识问答四项核心技能。另外,“雅意”全栈式出售服务模式可助力客户即刻拥有大模型的开发能力。资料显示,中科闻歌是一家中科院自动化所孵化的人工智能公司。
2025年05月26日
网络上相应的教程基本都基于LLaMA-Factory进行,本文章主要顺着相应的教程一步步实现大模型的微调和训练。
训练环境: 可自行定义, mac、linux 或者window之类的均可以, 本文以mac操作系统为例子进行。