Queue
Queue is a thread-safe queuing (FIFO) implementation in the Python standard library, providing a first-in-one data structure for multithreaded programming, a queue that is used to transfer information between producers and consumer threads
Basic FIFO queue
Class Queue.queue (maxsize=0)
FIFO is the first in. The queue provides a basic FIFO container, which is simple to use, and MaxSize is an integer that indicates the upper bound of the number of data that can be stored in the queue. Once the upper limit is reached, the insertion causes blocking until the data in the queue is consumed. If MaxSize is less than or equal to 0, there is no limit to the queue size.
Give me a chestnut:
Import= queue.queue () for in range (5): q.put (i)while not q.empty (): print q.get ()
Output:
01234
LIFO queue
Class Queue.lifoqueue (maxsize=0)
LIFO is last in first out, LIFO. Similar to the stack, the use is also very simple, maxsize usage ibid.
Give me another chestnut:
Import= queue.lifoqueue () for in range (5): q.put (i)while not q.empty (): print q.get ()
Output:
43210
You can see that just replacing the Queue.quenu class with the Queue.lifiqueue class
Priority queue
Class Queue.priorityqueue (maxsize=0)
Constructs a priority queue. MaxSize usage Ibid.
ImportQueueImportThreadingclassJob (object):def __init__(self, Priority, description): Self.priority=Priority Self.description=DescriptionPrint 'Job:', Descriptionreturn def __cmp__(self, Other):returnCMP (self.priority, other.priority) Q=Queue.priorityqueue () q.put (Job (3,'Level 3 Job')) Q.put (Job (10,'Level Job')) Q.put (Job (1,'Level 1 Job'))defprocess_job (q): whileTrue:next_job=Q.get ()Print 'For :', Next_job.description q.task_done () workers= [Threading. Thread (Target=process_job, args=(q,)), threading. Thread (Target=process_job, args=(q,))] forWinchWorkers:w.setdaemon (True) W.start () Q.join ()
Results
Job:level 31 jobfor: Level 1 jobfor: Level 3 Job for: Job
Some common methods Task_done ()
means that one of the previous missions has been completed. Called by the consumer thread of the queue. Each get () call gets a task, and the next Task_done () call tells the queue that the task has been processed.
If the current join () is blocking, it resumes execution when all the tasks in the queue have been processed (that is, every task queued by a put () call has a corresponding task_done () call).
Join ()
Blocks the calling thread until all tasks in the queue are processed.
As long as there is data queued, the number of unfinished tasks increases. The number of unfinished tasks is reduced when the consumer thread calls Task_done () (meaning that there is a consumer getting the task and completing the task). When the number of unfinished tasks drops to 0,join () is unblocked.
Put (item, block, timeout)
Put the item in the queue.
1. If the optional parameter block is true and timeout is an empty object (by default, blocking calls, no timeouts).
2. If timeout is a positive integer, block the calling process up to timeout seconds and throw a full exception (blocking call with timeout) if there is no space available.
3. If the block is false, throw the full exception immediately if there is free space available to put the data in the queue
Its non-blocking version is put_nowait () equivalent to put (item, False)
Get (block, timeout)
Removes from the queue and returns a data. Block with timeout parameter put method
Its non-blocking method is get_nowait () equivalent to get (False)
Empty ()
Returns true if the queue is empty, and vice versa returns false
Python queues queue