直接跑代码,看结果,结果在最后

from queue import Queue
from queue import PriorityQueue
print("Queue类实现了一个基本的先进先出(FIFO)容器,使用put()将元素添加到序列尾端,get()从队列尾部移除元素。\n")q = Queue()for i in range(3):q.put(i)while not q.empty():print(q.get())print("与标准FIFO实现Queue不同的是,LifoQueue使用后进先出序(会关联一个栈数据结构)。\n")from queue import LifoQueueq1 = LifoQueue()for i in range(3):q1.put(i)while not q1.empty():print(q1.get())print("除了按元素入列顺序外,有时需要根据队列中元素的特性来决定元素的处理顺序。例如,老板的打印任务可能比研发的打印任务优先级更高。PriorityQueue依据队列中内容的排序顺序(sort order)来决定那个元素将被检索。")class Job(object):def __init__(self, priority, description):self.priority = priorityself.description = descriptionprint('New job:', description)returndef __lt__(self, other):return self.priority < other.priorityq2 = PriorityQueue()q2.put(Job(5, 'Mid-level job'))
q2.put(Job(10, 'Low-level job'))
q2.put(Job(1, 'Important job')) #数字越小,优先级越高while not q2.empty():next_job = q2.get() #可根据优先级取序列print('Processing job', next_job.description)print(help(Queue))

以上代码输出:

Queue类实现了一个基本的先进先出(FIFO)容器,使用put()将元素添加到序列尾端,get()从队列尾部移除元素。

0
1
2
与标准FIFO实现Queue不同的是,LifoQueue使用后进先出序(会关联一个栈数据结构)。

2
1
0
除了按元素入列顺序外,有时需要根据队列中元素的特性来决定元素的处理顺序。例如,老板的打印任务可能比研发的打印任务优先级更高。PriorityQueue依据队列中内容的排序顺序(sort order)来决定那个元素将被检索。
New job: Mid-level job
New job: Low-level job
New job: Important job
Processing job Important job
Processing job Mid-level job
Processing job Low-level job

一下是类queue的系统解释:

Help on class Queue in module queue:

class Queue(builtins.object)
 |  Queue(maxsize=0)
 |  
 |  Create a queue object with a given maximum size.
 |  
 |  If maxsize is <= 0, the queue size is infinite.
 |  
 |  Methods defined here:
 |  
 |  __init__(self, maxsize=0)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  empty(self)
 |      Return True if the queue is empty, False otherwise (not reliable!).
 |      
 |      This method is likely to be removed at some point.  Use qsize() == 0
 |      as a direct substitute, but be aware that either approach risks a race
 |      condition where a queue can grow before the result of empty() or
 |      qsize() can be used.
 |      
 |      To create code that needs to wait for all queued tasks to be
 |      completed, the preferred technique is to use the join() method.
 |  
 |  full(self)
 |      Return True if the queue is full, False otherwise (not reliable!).
 |      
 |      This method is likely to be removed at some point.  Use qsize() >= n
 |      as a direct substitute, but be aware that either approach risks a race
 |      condition where a queue can shrink before the result of full() or
 |      qsize() can be used.
 |  
 |  get(self, 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 non-negative 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).
 |  
 |  get_nowait(self)
 |      Remove and return an item from the queue without blocking.
 |      
 |      Only get an item if one is immediately available. Otherwise
 |      raise the Empty exception.
 |  
 |  join(self)
 |      Blocks until all items in the Queue have been gotten and processed.
 |      
 |      The count of unfinished tasks goes up whenever an item is added to the
 |      queue. The count goes down whenever a consumer thread calls task_done()
 |      to indicate the item was retrieved and all work on it is complete.
 |      
 |      When the count of unfinished tasks drops to zero, join() unblocks.
 |  
 |  put(self, item, block=True, timeout=None)
 |      Put an 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 non-negative 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).
 |  
 |  put_nowait(self, item)
 |      Put an item into the queue without blocking.
 |      
 |      Only enqueue the item if a free slot is immediately available.
 |      Otherwise raise the Full exception.
 |  
 |  qsize(self)
 |      Return the approximate size of the queue (not reliable!).
 |  
 |  task_done(self)
 |      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.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

None
[Finished in 0.1s]

认识你是我们的缘分,同学,等等,记得关注我。

微信扫一扫
关注该公众号

Python3中queue模块的使用相关推荐

  1. Python3.5 queue 模块详解 和 进程间通讯

    queue - A synchronized queue class:https://docs.python.org/3/library/queue.html 菜鸟教程 - Python3 多线程:h ...

  2. pythonurllib模块-Python3中核心模块urllib的用法大全

    Python的urllib模块提供了一个高级的Web通信库,支持基本的协议,如HTTP.FTP和Gopher,同时也支持对本地文件的访问.在Python3中,urllib.urlparse.urlli ...

  3. python3中argparse模块详解

    文章目录 python3中argparse模块详解 一. 命令行参数分为位置参数和选项参数: 二. 使用步骤: 三. add_argument()方法参数: 1. name or flags: 2. ...

  4. Python3中typing模块介绍

    typing.py的源码在:https://github.com/python/cpython/blob/main/Lib/typing.py.此模块为类型提示(Type Hints)提供运行时支持( ...

  5. python3中 operator模块用法介绍

    文章目录 概述 将运算符映射到函数 1基本方法 介绍 2 三个类介绍 2-1 attrgetter 介绍 2-2 itemgetter 使用介绍 1 用来排序 2 通过 获取多个值 2-3 metho ...

  6. Python3中pickle模块介绍

          Pyhton3中的pickle模块用于对Python对象结构的二进制进行序列化(或pickling)和反序列化(或unpickling)."pickling"是将Pyt ...

  7. Python3 中 random模块

    Python中的random模块用于生成随机数. 下面具体介绍random模块的功能: 1.random.random() #用于生成一个0到1的随机浮点数:0<= n < 1.0 imp ...

  8. python3中argparse模块

    1.定义:argparse是python标准库里面用来处理命令行参数的库 2.命令行参数分为位置参数和选项参数: 位置参数就是程序根据该参数出现的位置来确定的 如:[root@openstack_1  ...

  9. python中zipfile的使用_详解python3中zipfile模块用法

    一.zipfile模块的简述 zipfile是python里用来做zip格式编码的压缩和解压缩的,由于是很常见的zip格式,所以这个模块使用频率也是比较高的, 在这里对zipfile的使用方法做一些记 ...

最新文章

  1. 在CentOS 6.9上从源码安装Go 1.10
  2. Java 枚举类的基本使用
  3. bat文件设置dns服务器,.bat文件设置IP、DNS
  4. 【C++基础】异常处理机制概要
  5. 如何快速查找下载java项目所需jar包
  6. flask导入flask-sqlalchemy的一个问题记录
  7. Java基础学习总结(157)——国产开源JDK来了
  8. io读取一个文件再写入socket技术_Kafka必须掌握的核心技术--为什么吞吐量大、速度快?...
  9. Haproxy+keepalived高可用集群实战
  10. hadoop问题锦集(一)-搭建集群环境时的常见问题
  11. 二分图的最大匹配--匈牙利算法
  12. SCVMM 2012 R2---添加Hyper-V虚拟机
  13. 码云仓库第一次上传代码流程和git相关操作合集(持续更新)
  14. 外资餐饮连锁或因产品质量影响销售额
  15. python语言for else和 if else误区
  16. 漫画:用木兰从军说外观模式
  17. QT 读取csv文件-QT根据显示器大小设置窗口大小-QT绑定信号与槽
  18. 刑侦科推理试题(不知来源,最右APP看到)
  19. Go 小项目1 - 家庭收支记账软件
  20. 爬虫:动态页面爬取Selenium

热门文章

  1. Leetcode-最长回文子串(包含动态规划以及Manacher算法)
  2. elasticsearch 部署
  3. Cheatsheet: 2015 03.01 ~ 03.31
  4. 一维数组的初始化问题
  5. CentOS升级Python到2.7版本
  6. POJ-3469 Dual Core CPU 最小割最大流
  7. 一步一步安装SQL Server 2008(附截图)
  8. (转)淘淘商城系列——使用solrj来测试索引库
  9. 【UML】活动图Activity diagram(转)
  10. 笨办法学 Python · 续 练习 33:解析器