In python thread queue Queue-FIFO article has been introduced in the FIFO queue Queue, and to tell you today is the second: the thread queue LifoQueue-LIFO, last-out type of data , both what difference does it make?
A queue Queue Category:
1. thread queue Queue - the 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;
Today, only the second thread queue Queue (LIFO) explain, the last one to leave in the next article detailed explanation!
II., Last-out queue LifoQueue Profile
As described above, the contrary, the last stored data before a first extraction with the Queue, the stored first data of the last taken, as shown below:
If the FIFO is pulling what to eat, what to eat what was then the LIFO spit, eat after the spit, vomit after eating the first really heavy taste ah ~ ~!
III., Last-out queue LifoQueue function introduction
Function not introduce too much, has been in python thread queue Queue-FIFO with detailed explanations, both of which belong to the Queue, functions are the same!
4. When the queue Queue-LIFO advanced use
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:何以解忧 @Blog(个人博客地址): shuopython.com @WeChat Official Account(微信公众号):猿说python @Github:www.github.com @File:python_LifoQueue.py @Time:2019/11/29 15:25 @Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! """ import queue import threading import time # 可以设置队列的长度 q=queue.LifoQueue(5),意味着队列中最多存放5个元素,当队列满的时候自动进入阻塞状态 q=queue.LifoQueue() def put(): for i in range(10): q.put(i) print("数据%d被存入到队列中" % i) q.join() print('ok') def get(): for i in range(10): value = q.get() print("数据%d从队列中取出" % value) q.task_done() t1=threading.Thread(target=put,args=()) t1.start() t2=threading.Thread(target=get,args=()) t2.start()
输出结果:
0 is stored into the data queue
data is stored in a queue to
the data 2 is stored into the queue
of data into the queue 3 is stored in
the data queue 4 is stored into
the data 5 are stored into the queue
data 6 is stored into the queue
data 7 is stored into the queue
data is stored into the queue 8
data 9 are stored into the queue
data 9 dequeues
data 8 dequeues
data 7 dequeues
data 6 dequeues
data 5 dequeues
the data extracted from the queue 4
of data extracted from the queue 3
data extracted from the queue 2
taken out from the queue data
data 0 dequeues
ok
you may also like:
1.python thread queue Queue-FIFO
3.python __name__ == '__main__' Detailed Explanation
4.python variable length parameters * argc, ** kargcs
Reproduced please specify: ape say Python » Python thread queue LifoQueue-LIFO