Python3.7.3
from queue import Queue,LifoQueue,PriorityQueue
q =Queue(maxsize=0)
q =LifoQueue(maxsize=0)
q =PriorityQueue(maxsize=0)
# More details later
maxsize: maxsize is an integer that specifies the upper limit of the number of data that can be stored in the queue. Once the upper limit is reached, insertion will cause blocking until the data in the queue is consumed. If maxsize is less than or equal to 0, there is no limit on the queue size.
put(item, block=True, timeout=None)
# item:Enrolled data(Any data type is fine)
# block:bool type,Default True
# timeout:overtime time,Default None
# 1. When block is the default value,If the queue is already in"Full team"status,Also continue to insert data into the queue,At this time, the value of timeout is the time when the program throws an exception(timeout=When None,The program is always in"Blockage"status,Unless there is data"Leave the team")
# 2. When block=When False,No matter what the timeout is,Just queue"Blockage"Throw an exception immediately
get(block=True, timeout=None)
# block:bool type,Default True
# timeout:overtime time,Default None
# 1. When block is the default value(True),If the queue is already in"air"status,Have to continue"Leave the team",At this time, the value of timeout is the time when the program throws an exception(timeout=When None,The program is always in"air转"status(Infinite loop),Unless there is data"Join the team")
# 2. When block=When False,No matter what the timeout is,Just queue"Idling"Throw an exception immediately
q.qsize()
# If the queue is empty, return True,Otherwise False
q.empty()
# If the queue is full, return True,Otherwise False
q.full()
q.queue.clear()
from queue import PriorityQueue
q =PriorityQueue()classJob(object):
def __init__(self, priority, description):
self.priority = priority
self.description = description
print('Join the team:', description)return
def __lt__(self, other):return self.priority < other.priority
# The lower the number, the higher the priority
q.put(Job(1,"aaa"))
q.put(Job(2,"bbb"))
q.put(Job(8,"ccc"))
q.put(Job(3,"ddd"))print(q.get().description)print(q.get().description)print(q.get().description)print(q.get().description)
Recommended Posts