一、线程队列

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

'''

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程序中的线程操作-线程队列相关推荐

  1. python打开一个软件并进行操作_python程序中的进程操作

    之前我们已经了解了很多进程相关的理论知识,了解进程是什么应该不再困难了,刚刚我们已经了解了,运行中的程序就是一个进程.所有的进程都是通过它的父进程来创建的.因此,运行起来的python程序也是一个进程 ...

  2. python启动多个进程_Python程序中的进程操作--—--开启多进程

    Python程序中的进程操作-----开启多进程 之前我们已经了解了很多进程相关的理论知识,了解进程是什么应该不再困难了,刚刚我们已经了解了,运行中的程序就是一个进程.所有的进程都是通过它的父进程来创 ...

  3. python进程数据共享_python程序中的进程操作-进程间的数据共享

    展望未来,基于消息传递的并发编程是大势所趋 即便是使用线程,推荐做法也是将程序设计为大量独立的线程集合,通过消息队列交换数据. 这样极大地减少了对使用锁定和其他同步手段的需求,还可以扩展到分布式系统中 ...

  4. python多线程和多进程的使用_python多线程与多进程

    python多线程与多进程 python多线程 python中提供两个标准库thread和threading用于对线程的支持,python3中已放弃对前者的支持,后者是一种更高层次封装的线程库,接下来 ...

  5. 在python程序中的进程操作

    ********在python程序中的进程操作********之前我们已经了解了很多进程相关的理论知识,了解进程是什么应该不再困难了,刚刚我们已经了解了, 运行中的程序就是一个进程.所有的进程都是通过 ...

  6. python在中小学教学中的应用-在python程序中的进程操作

    ********在python程序中的进程操作********之前我们已经了解了很多进程相关的理论知识,了解进程是什么应该不再困难了,刚刚我们已经了解了, 运行中的程序就是一个进程.所有的进程都是通过 ...

  7. C++核心准则ES.30: 不要使用宏进行程序中的文本操作​

    ES.30: Don't use macros for program text manipulation ES.30: 不要使用宏进行程序中的文本操作 Reason(原因) Macros are a ...

  8. python多线程和多进程的区别_python中多线程与多进程的区别

    线程的概念: 线程是操作系统中进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一条线程指的是进程中一个单一顺序的控制流,一个进程可以有多个线程,每条线程可以同时执行不同的任务.一个 ...

  9. python 多线程和协程结合_Python 异步编程,看这门课就够了~

    我们常见的 Linux.Windows.Mac OS 操作系统,都是支持多进程的多核操作系统.所谓多进程,就是系统可以同时运行多个任务.例如我们的电脑上运行着 QQ.浏览器.音乐播放器.影音播放器等. ...

最新文章

  1. grails 转为java_创建一个grails项目,然后转成maven项目
  2. firework常用快捷键
  3. WebP 在减少图片体积和流量上的效果如何?WebP 技术实践分享
  4. <笔记>光纤(光波导)中的传输特性1
  5. linux 网卡 虚拟化,RHEL6.4 KVM虚拟化网卡桥接,PXE无人值守安装虚拟机
  6. JAVA常用的工具类
  7. UA OPTI512R 傅立叶光学导论14 卷积定理
  8. solidity教程-简介
  9. gmod服务器文件,gmod服务器里改名字指令 | 手游网游页游攻略大全
  10. 基于SSM实现个性化健康饮食推荐系统
  11. TCP/IP协议分析实验
  12. Clickhouse 踩坑之旅 ---- MergeTree不合并分区的问题
  13. PACS(CT、CR、DR、MR、DSA、RF医院影像管理系统源码)
  14. 【无标题】5421码 2421码 余三码
  15. 【BZOJ 1064】【NOI 2008】假面舞会
  16. ts报错:Object is possibly ‘undefined‘.Vetur(2532)
  17. STM32CUBEIDE(11)----输出PWM及修改PWM频率与占空比
  18. STM32CubeIDE开发(三), stm32应用开发过程涉及的术语简称表
  19. 基于Python(Django)+MySQL实现(Web)大学生信用评价系统分析与设计【100010400】
  20. KeyBert关键词提取 :原理、方法介绍、代码实践

热门文章

  1. 离线java人脸识别开源算法_Java 离线人脸识别 基于ArcFace 2.0 Demo
  2. js ajax通用方法,ajax的四种实现方式介绍
  3. 关于计算机的作文初一,关于初一作文汇编五篇
  4. arm linux 进程页表,linux-kernel – ARM Linux页表项格式 – 未使用的位?
  5. 卸料装置弹性零件的计算方法_冲裁力、卸料力及推件力的计算-常见问题.doc
  6. python 爬虫 请求网页内容不一致_Python爬虫偷懒神器 — 快速构造请求头!
  7. echo print php,从0到1学习网络安全 【PHP基础-输出(echo/print)】
  8. java全jit编译_JVM即时编译(JIT)(转载)
  9. torque安装笔记
  10. CSS3 Media Query:移动 Web 的完美开端