【10.0】线程queue
【零】队列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被消费完毕
本文来自博客园,作者:Chimengmeng,转载请注明原文链接:https://www.cnblogs.com/dream-ze/p/17982400