程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Three usages of thread queue

編輯:Python

The point of this section

Master threads queue Three uses of
The duration of this section needs to be controlled 5 Within minutes

One Threads queue

queue is especially useful in threaded programming when information must be exchanged safely between multiple threads.

There are three different uses

class queue.Queue(maxsize=0) # queue : fifo
import queue
q=queue.Queue()
q.put('first')
q.put('second')
q.put('third')
print(q.get())
print(q.get())
print(q.get())
'''
result ( fifo ):
first
second
third
'''

class queue.LifoQueue(maxsize=0) # Stack :last in fisrt out

import queue
q=queue.LifoQueue()
q.put('first')
q.put('second')
q.put('third')
print(q.get())
print(q.get())
print(q.get())
'''
result ( Last in, first out ):
third
second
first
'''

class queue.PriorityQueue(maxsize=0) # Priority queue : Priority queues can be set when storing data

import queue
q=queue.PriorityQueue()
#put Enter a tuple , The first element of a tuple is priority ( It's usually numbers , It can also be a comparison between numbers ), The smaller the number, the higher the priority
q.put((20,'a'))
q.put((10,'b'))
q.put((30,'c'))
print(q.get())
print(q.get())
print(q.get())
'''
result ( The smaller the number, the higher the priority , A high priority team ):
(10, 'b')
(20, 'a')
(30, 'c')
'''

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved