In the thread queue Queue / thread queue LifoQueue article introduced the FIFO queue Queue out and the advanced queue LifoQueue, and today to tell you that last: a priority queue PriorityQueue, sort the data according to the priority queue, so specifically how to use it?
A queue Queue Category:
1. thread queue Queue - FIFO (First In First Out queue), ie, which data is first stored, the time taken to fetch data which data, buy things with life line;
2. Thread the queue LifoQueue - LIFO (last-out queue), that is, the last stored data which, when taken in the first data take the same life magazine pistol, bullets and finally into the first play;
3. thread queue PriorityQueue - PriorityQueue (priority queue), i.e., when the added data is stored in a priority, when the highest priority of access to data extraction;
II. Introduction priority queue PriorityQueue
Set the priority at the time the data is stored, the time taken in accordance with the data of the default highest priority out, Note: Use the priority data stored fetch data, the data queue must be of the same type, give chestnuts: Class standings / Height rank ......
It is noted that: when the data stored in the priority queue PriorityQueue, the smaller the value, the higher the priority;
III. PriorityQueue priority queue function introduction
Function not introduce too much, already in the thread queue Queue with detailed explanations, both of which belong to the Queue, functions are the same!
IV. PriorityQueue use priority queue
Priority: whether numbers, letters, lists, and other tuples (dictionaries, set did not test), using the data stored in the priority access to data, data queue must be of the same type, ascii code table are in accordance with actual data order of priority matching, in accordance with the unicode characters table.
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:何以解忧 @Blog(个人博客地址): shuopython.com @WeChat Official Account(微信公众号):猿说python @Github:www.github.com @File:python_priorityqueue.py @Time:2019/11/29 1queue5:25 @Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! """ import queue import threading import time q = queue.PriorityQueue() q.put([1, 'ace']) q.put([40, 333]) q.put([3, 'afd']) q.put([5, '4asdg']) # 1是级别最高的, while not q.empty(): # 不为空时候执行 print(q.get()) q = queue.PriorityQueue() q.put('我') q.put('你') q.put('他') q.put('她') q.put('ta') while not q.empty(): print(q.get())
Output:
[1, 'ace'] [ 3, ' Department ' ] [5, '4asdg'] [40, 333] ta he you she was I
Thus: When the data stored in the priority queue PriorityQueue, the smaller the value, the higher the priority!
you may also like:
1.python thread queue Queue-FIFO
2.python thread queue LifoQueue
Reproduced please specify: ape say Python » Python thread queue PriorityQueue (priority queue)