python 阻塞队列_Python程序中的线程操作-线程队列

Python程序中的线程操作-线程队列

一、线程队列

queue队列:使用import queue,用法和进程Queue一样

当必须在多个线程之间安全地交换信息时,队列在线程编程中尤其有用。

二、先进先出

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

'''

三、后进先出

class queue.LifoQueue(maxsize=0)

import queue

q=queue.LifoQueue()

q.put('first')

q.put('second')

q.put('third')

print(q.get())

print(q.get())

print(q.get())

'''

结果(后进先出):

third

second

first

'''

四、存储数据时可设置优先级的队列

class queue.PriorityQueue(maxsize=0)

4.1优先级队列

import queue

q=queue.PriorityQueue()

#put进入一个元组,元组的第一个元素是优先级(通常是数字,也可以是非数字之间的比较),数字越小优先级越高

q.put((20,'a'))

q.put((10,'b'))

q.put((30,'c'))

print(q.get())

print(q.get())

print(q.get())

'''

结果(数字越小优先级越高,优先级高的优先出队):

(10, 'b')

(20, 'a')

(30, 'c')

'''

4.2更多方法说明

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.

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).

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

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

Queue.qsize()

Queue.empty(): return True if empty

Queue.full(): return True if full

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).

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).

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.

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).

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

Queue.join(): block直到queue被消费完毕。

翻译

优先级队列的构造函数。

maxsize是一个整数,它设置可以放置在队列中的项数的上限。

一旦达到此大小,插入将阻塞,直到使用队列项。

如果maxsize小于或等于零,则队列大小为无穷大。

首先检索值最低的条目(值最低的条目是由已排序(列表(条目))[0]返回的条目)。

条目的典型模式是元组的形式:(priority_number, data)。

exception queue.Empty:在空队列对象上调用非阻塞get()(或get_nowait())时引发异常。

exception queue.Full:当对已满的队列对象调用非阻塞put()(或put_nowait())时引发异常。

Queue.qsize()

Queue.empty():如果为空返回True

Queue.full():如果已满返回True

Queue.put(item, block=True, timeout=None):将项放入队列。

如果可选的args块为true, timeout为None(缺省值),则在空闲插槽可用之前,如果有必要,将阻塞。

如果timeout是一个正数,那么它将阻塞最多的超时秒,如果在这段时间内没有可用的空闲插槽,则引发完整的异常。

否则(block为false),如果一个空闲插槽立即可用,则将一个项放到队列中,否则引发完全异常(在这种情况下忽略超时)。

Queue.put_nowait(item):相当于put(item, False)。

Queue.get(block=True, timeout=None):从队列中删除并返回一个项。

如果可选的args块为true, timeout为None(缺省值),则在项目可用之前,如果有必要,将阻塞。

如果timeout是一个正数,那么它将阻塞最多的超时秒,如果在这段时间内没有可用的项,则引发空异常。

否则(block为false),返回一个立即可用的项,否则引发空异常(在这种情况下忽略超时)。

Queue.get_nowait():相当于(False)。

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

Queue.task_done():指示已完成先前排队的任务。

由队列使用者线程使用。

对于用于获取任务的每个get(),后续对task_done()的调用告诉队列任务上的处理已经完成。

如果join()当前处于阻塞状态,那么在处理完所有项之后,它将继续运行(这意味着对于已经放入队列()的每个项,都收到了task_done()调用)。

如果调用次数超过放置在队列中的项的次数,则引发ValueError。

Queue.join(): block直到queue被消费完毕。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值