首页 > 其他分享 >【10.0】线程queue

【10.0】线程queue

时间:2024-01-23 14:34:51浏览次数:22  
标签:10.0 get 队列 Queue queue 线程 timeout put

【零】队列queue介绍

  • queue队列 :
    • 使用import queue,用法与进程Queue一样
  • queue is especially useful in threaded programming when information must be exchanged safely between multiple threads.
  • class queue.Queue(maxsize=0) 先进先出
import queue

q=queue.Queue()
q.put('first')
q.put('second')
q.put('third')

print(q.get())
print(q.get())
print(q.get())
'''
结果(先进先出):
first
second
third
'''

【一】线程queue介绍

  • 同一个进程下的多个线程数据是共享的
  • 为什么在同一个进程下还要使用队列?
    • 因为队列 = 管道 + 锁
    • 使用队列是为了保证数据的安全

【二】后进先出

import queue

# 我们现在使用的队列都是只能在本地测试使用

# 队列q:先进先出
'''
# 队列的常用方法
q = queue.Queue(3)
q.put(1)
q.get()
q.get_nowait()
q.get(timeout=3)
q.full()
q.empty()
'''

# (1)后进先出q
q = queue.LifoQueue(3)
q.put(1)
q.put(2)
q.put(3)

print(q.get())
# 3

【三】PriorityQueue设置队列优先级

  • 数字越小优先级越高
import queue

# (2)优先级 q
# 可以给防止队列中的数据设置队列优先级
q = queue.PriorityQueue(3)
# (优先级,参数)
q.put((10, '111'))
q.put((100, '222'))
q.put((0, '333'))
q.put((-5, '444'))
print(q.get())

# (-5, '444')
# (优先级, 参数)
# 数字越小优先级越高

【四】拓展

Constructor for a priority queue. maxsize is an integer that sets the upperbound limit on the number of items that can be placed in the queue. Insertion will block once this size has been reached, until queue items are consumed. If maxsize is less than or equal to zero, the queue size is infinite.

构造一个优先级队列,其中maxsize是一个整数,用于设置可以放入队列的项目数量的上限.一旦达到这个上限,插入就会阻塞,直到队列中有项目被消耗。如果maxsize小于或等于0,则队列长度为无穷大。

The lowest valued entries are retrieved first (the lowest valued entry is the one returned by sorted(list(entries))[0]). A typical pattern for entries is a tuple in the form: (priority_number, data).

首先检索最低值的条目(最低值的条目是指列表经过排序后取到的索引为0的那个元素,一般条目是(优先级数字,数据)这种元组的形式

exception queue.Empty
Exception raised when non-blocking get() (or get_nowait()) is called on a Queue object which is empty.

当表示非阻塞的get()或get_nowait()在一个空的队列对象中被调用时,会抛出异常

exception queue.Full
Exception raised when non-blocking put() (or put_nowait()) is called on a Queue object which is full.

当表示非阻塞的put()或put_nowait()在一个满的队列对象中被调用时,会抛出异常

Queue.qsize()
Queue.empty() #return True if empty  

当队列为空返回True

Queue.full() # return True if full 

当队列为满返回True

Queue.put(item, block=True, timeout=None)
Put item into the queue. If optional args block is true and timeout is None (the default), block if necessary until a free slot is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Full exception if no free slot was available within that time. Otherwise (block is false), put an item on the queue if a free slot is immediately available, else raise the Full exception (timeout is ignored in that case).

将一个项放入队列。如果可选参数block为true并且timeout为None(默认值),则在必要时阻塞,直到有空闲槽可用。如果参数timeout是一个正数,它最多阻塞timeout秒,如果在这段时间内没有可用的空闲槽,则会引发Full异常。否则(block为false),如果有空闲槽可用,则将一个项目放入队列中,否则引发Full异常(在这种情况下,timeout被忽略)。

Queue.put_nowait(item)
Equivalent to put(item, False).

Queue.get(block=True, timeout=None)
Remove and return an item from the queue. If optional args block is true and timeout is None (the default), block if necessary until an item is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Empty exception if no item was available within that time. Otherwise (block is false), return an item if one is immediately available, else raise the Empty exception (timeout is ignored in that case).

从队列中移除并返回一个项。如果可选参数block为true并且timeout为None(默认值),则在必要时阻塞,直到有可用的项。如果timeout为正数,则最多阻塞timeout秒,如果在该时间内没有可用项,则抛出Empty异常。否则(block为false),如果一个项目可用,则返回那个项目,否则引发Empty异常(在这种情况下,timeout被忽略)。

Queue.get_nowait()
Equivalent to get(False).

Two methods are offered to support tracking whether enqueued tasks have been fully processed by daemon consumer threads.

提供了两种方法来支持追踪进入队列的任务是否已被生产者的守护线程完全处理。

Queue.task_done()
Indicate that a formerly enqueued task is complete. Used by queue consumer threads. For each get() used to fetch a task, a subsequent call to task_done() tells the queue that the processing on the task is complete.

假定先前进入队列的任务已完成。并且被队列生产者使用。对于每个用于获取任务的get(),后续对task_done()的调用都会告诉队列任务的处理已经完成。

If a join() is currently blocking, it will resume when all items have been processed (meaning that a task_done() call was received for every item that had been put() into the queue).

如果join()当前正被阻塞,它将在所有项都被处理完时恢复(这意味着对于每个已经put()到队列中的项都接收到task_done()调用)。

Raises a ValueError if called more times than there were items placed in the queue.

如果调用次数超过放入队列的项数,将引发ValueError。

Queue.join() 

阻塞,直到queue被消费完毕

标签:10.0,get,队列,Queue,queue,线程,timeout,put
From: https://www.cnblogs.com/dream-ze/p/17982400

相关文章

  • 线程同步之信号量
    目录Semaphore信号量Semaphore与condition_variable的区别Semaphore信号量在C++中,可以使用std::Semaphore类来实现信号量。信号量可以用于控制对资源的访问,例如限制同时执行任务的线程数量。在C++11中,std::Semaphore类提供了以下常用函数:Semaphore():构造函数,创建一个信......
  • MySQL线程状态详解
    前言:我们常用showprocesslist或showfullprocesslist查看数据库连接状态,其中比较关注的是State列,此列表示该连接此刻所在的状态。那么你真的了解不同State值所表示的状态吗?下面我们参考官方文档来一探究竟。以MySQL5.7版本为例官方文档地址:https://dev.my......
  • QT笔记:多线程和信号槽
    QT笔记:多线程和信号槽多线程创建多线程有两种方法,一般推荐用moveToThread方法参考代码如下:mainwindow.h#ifndefMAINWINDOW_H#defineMAINWINDOW_H#include<QMainWindow>#include<QApplication>QT_BEGIN_NAMESPACEnamespaceUi{classMainWindow;}QT_END_NAMES......
  • .NET 6 实现一个任务队列,且在不同线程中调用队列,队列始终都是串行执行
    在.NET6中,要实现一个任务队列,确保队列中的任务始终串行执行,即使它们是由不同线程调用的,你可以使用Channel<T>结合Task.Run或者更简单地使用BlockingCollection<T>与Task.Factory.StartNew或async/await模式。不过,为了保持代码的简洁性和现代性,我会推荐使用Channel<T>结合async/aw......
  • STL-stack和queue堆栈和队列
    STL-stack和queue堆栈和队列目录STL-stack和queue堆栈和队列堆栈和队列特性堆栈主要操作构造函数主要操作栈顶插入和删除大小相关简单案例队列的主要操作构造函数大小相关索引访问入队/出队优先队列priority_queue初始化构造小顶堆自定义结构体排序参考资料堆栈和队列特性stack......
  • 线程池
    目录简单的线程池简单的线程池在C++11中,你可以使用 std::thread 和 std::mutex 等标准库来实现一个简单的线程池。以下是一个简单的示例代码:#include<iostream>#include<vector>#include<thread>#include<mutex>#include<queue>classThreadPool{private:......
  • java线程池-1
    1.概述Java线程的创建非常昂贵,需要JVM和OS(操作系统)配合完成大量的工作:必须为线程堆栈分配和初始化大量内存块,其中包含至少1MB的栈内存。需要进行系统调用,以便在OS(操作系统)中创建和注册本地线程。Java高并发应用频繁创建和销毁线程的操作将是非常低效的,而且是不被......
  • C++中lambda与priority_queue一起使用
    想写这篇博客的原因是在刷力扣的347.前K个高频元素一题时,需要使用到优先队列priority_queue,其定义如下:template<classT,classContainer=std::vector<T>,classCompare=std::less<typenameContainer::value_type>>classpriority_queue;第三个参数......
  • Queue-Linked List Implementation【1月22日学习笔记】
    点击查看代码//Queue-LinkedListImplementation#include<iostream>usingnamespacestd;structnode{ intdata; node*next;};node*front=NULL;node*rear=NULL;//末指针·,则不用遍历整个链表,constanttimevoidEnqueue(intx){ node*temp=newnode; ......
  • python 多线程multiprocessing
    该多线程,简单计算结果可以使用,在django里想并行处理多个实体进行计算不行,请自行验证importmultiprocessing#要在进程池中并行执行的任务函数defprocess_data(data):#执行任务的逻辑result=data*2returnresultif__name__=='__main__':#创......