Python程序中的线程操作-线程队列
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python程序中的线程操作-线程队列,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3903字,纯文字阅读大概需要6分钟。
内容图文
![Python程序中的线程操作-线程队列](/upload/InfoBanner/zyjiaocheng/644/3dc47a908d0142cea40602f3b6d2a45b.jpg)
目录
Python程序中的线程操作-线程队列
一、线程队列
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
'''
三、后进先出
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')
'''
定时器
from threading import Timer
import time
def task():
print("线程执行")
time.sleep(2)
print("线程结束")
# 过了3s后开启了一个线程
t = Timer(4, task)
t.start()
print(3)
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被消费完毕。
内容总结
以上是互联网集市为您收集整理的Python程序中的线程操作-线程队列全部内容,希望文章能够帮你解决Python程序中的线程操作-线程队列所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。