Queue-FIFO Implementation of thread security
Role: provides a thread-safe FIFO implementation
The queue module provides a first-in, first-out (first-out) data structure for multi-threaded programming. It can be used to safely transmit messages or other data between the producer and consumer threads. It will lock the caller, so that multiple threads can safely process the same queue instance. The size of the queue (including the number of elements) may be limited to restrict memory usage or processing.
1. Basic FIFO queue
The queue class implements a first-in-first-out container that is basically not available. Add an element to a sequence using put () and delete it from the other end using get.
1 import queue 2 3 Q = queue. lifoqueue () 4 A = range (5) 5 print U' list of forward orders: ', a 6 for I IN A: 7 Q. put (I) 8 print U': '9 while not Q. empty (): 10 print Q. get (), 11 print12 13 A = range (5) 14. sort (reverse = true) 15 print u'reverse list: ', A16 for I IN A: 17 Q. put (I) 18 print U': '19 while not Q. empty (): 20 print Q. get (), 21 print
Display result:
This example uses a thread to show how to delete elements from the queue in the insert order of elements.
2. LIFO queue
Contrary to the standard FIFO Implementation of queue, lifoqueue uses the last-in, first-out, and LIFO sequence (usually associated with the stack data structure ).
1 import queue 2 3 Q = queue. queue () 4 A = range (5) 5 print U' list in the forward direction: ', a 6 for I IN A: 7 Q. put (I) 8 print U': '9 while not Q. empty (): 10 print Q. get (), 11 print12 13 A = range (5) 14. sort (reverse = true) 15 print u'reverse list: ', A16 for I IN A: 17 Q. put (I) 18 print U': '19 while not Q. empty (): 20 print Q. get (), 21 print
Running result:
Get deletes the elements recently inserted into the queue using put.
Task_cone ()
After a task is completed, the queue. task_done () function sends a signal to the completed queue of the task.
Join ()
It actually means that when the queue is empty, other operations will be performed.
3. priority queue
In some cases, the processing sequence of elements in the queue depends on the particularity of these elements, not just the sequence of creation or insertion in the queue. For example, a printing job in the finance department may take precedence over a code list printed by a developer. Priorityqueue uses the ordered order of queue content to determine which element to obtain.
1 import Queue 2 import threading 3 4 class Job(object): 5 def __init__(self,priority,description): 6 self.priority = priority 7 self.description = description 8 print ‘New job:‘,description 9 return 10 def __cmp__(self,other):11 # print ‘a:‘,self.priority12 # print ‘b:‘,other.priority13 # print cmp(self.priority,other.priority)14 return cmp(self.priority,other.priority)15 16 17 q = Queue.PriorityQueue()18 q.put(Job(3,‘Mid-level job‘))19 q.put(Job(10,‘Low-level job‘))20 q.put(Job(1,‘Important job‘))21 22 def process_job(q):23 while True:24 next_job = q.get()25 print ‘Processing job:‘,next_job.description26 q.task_done()27 28 workers = [threading.Thread(target = process_job,args = (q,)),29 threading.Thread(target = process_job,args = (q,)),30 ]31 for w in workers:32 w.setDaemon(True)33 w.start()34 q.join()
Running result:
In this example, a multi-thread processing job needs to be processed based on the priority of the elements in the queue during get. When a consumer thread is running, the processing order of the elements added to the queue is determined by the thread context switch.
Daily "cool" queue