First, queue queues
Queues queue multiple applications in multi-threaded scenarios, multi-threaded access to shared variables.
For multi-threading, queue queues are thread-safe when accessing shared variables.
Because the queue uses a thread lock (pthread. Lock ()), and three condition variables (pthread.condition ()) to ensure thread safety.
Summary: Queues provide a secure and reliable shared data usage scenario.
Queue built-in control security of several parameters, non-user use |
Name |
Role |
Self.mutex |
Mutual exclusion Lock |
Any operation that gets the state of the queue (empty (), qsize (), and so on), or modifies the contents of the queue (Get,put, etc.) must hold the mutex. There are two kinds of operation require get lock, release releasing lock. At the same time, the mutex is enjoyed by three shared variables, that is, the require and release operation of the Conditiond operation is the mutex. |
Self.not_full |
Condition variable The queue is not full |
When a queue has elements added, it notifies notify of other threads waiting to add elements, wakes up to wait for the require mutex, or notifies other threads to wake up to wait for the require mutex after the thread thread pulls an element out of the queue. |
Self.not_empty |
Condition variable The queue is not empty |
After the thread adds data to the queue, it calls Self.not_empty.notify () to notify the other threads, wake up waiting for the require mutex, and read the queue. |
Self.all_tasks_done |
Condition variable Queue data is all processed |
After the consumer thread has been removed from the queue to the task, the task processing is completed, and when all the tasks in the queue are processed, the thread that calls Queue.join () is returned, indicating that the task in the queue is processed. |
# # #queue的初始化函数 # # #def __init__ (self, maxsize=0): Self.maxsize = maxsize self._init (maxsize) # mutex Must be held whenever the queue is mutating. all methods # that acquire mutex must release it before returning. mutex # is shared between the three conditions, so acquiring and # releasing the conditions also acquires and releases mutex. &nbSp; self.mutex = _threading. Lock () # notify not_empty whenever an item is added to the queue; a # thread waiting to get is notified then. self.not_empty = _threading. Condition (Self.mutex) # Notify not_full whenever an item is removed from the queue; # a thread waiting to put is notified then. self.not_full = _ Threading. Condition (Self.mutex) # Notify all_tasks_done whenever the number of unfinished tasks # Drops to zero; thread waiting to join () is notified to resume self.all_tasks_done = _threading. Condition (Self.mutex) self.unfinished_ Tasks = 0
Second, the queue data access rules:
How data is used
|
Class name
|
Role |
Example |
fifo FIFO |
queue (maxsize) |
First enter the queue data, first remove maxsize:>=0 Set Queue Length, 0 is infinite length |
q = queue. Queue () |
filo Advanced back Out |
(maxsize) |
First enter the queue data, and finally take out m Axsize:>=0 set queue Length, 0 is infinite length |
q = queue. Lifoqueue () |
Priority level
|
Priorityqueue (maxsize)
|
Set priority flag, take priority to remove high mark Maxsize:>=0
Set Queue Length, 0 for infinite length
|
Q = queue. Priorityqueue ()
|
# # #例子一: Advanced First Out # # #import queueq = queue. Queue () For i in range (5): q.put (i) for i in range (5): print (Q.get (), end= " ") #---results---0 1 2 3 4 ## #例子二: Last in First out # # #import queueq = queue. Lifoqueue () For i in range (5): q.put (i) For i in range (5): print (Q.get (), end= " ") #---results---4 3 2 1 0## #例子三: Read # # # by Priority flag bit Refer to other information to see many of the implementation of the priority queue, but I think the way to directly use tuples is relatively simple and rude. Import queuep = queue. Priorityqueue () P.put ((3, "3")) P.put ((1, "1")) P.put ((4, "4")) P.put ((2, "2")) For i in range (3): print (P.get ()) #---Results: Sort by tuple index 0--- (1, ' 1 ') (2, ' 2 ') (3, ' 3 ') (4, ' 4 ') ## #例子四: Multivariate group judgment ### import Queuep = queue. Priorityqueue () p.put (1,4, "a")) P.put ((2,1, "666")) P.put ((1,3, "4")) P.put ((2,2, "2")) For i in range (3): print (P.get ()) #-----Result: The corresponding ordinal of the tuple is compared, the primary key is the ordinal 0, the more backward, the lower the priority. -----(1, 3, ' 4 ') (1, 4, ' a ') (2, 1, ' 666 ') (2, 2, ' 2 ')
three , common methods and properties for queues:
Methods and properties |
Role
|
Example
|
Task_done () |
1, a task before the mark has been completed. 2. 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. 3. If the current join () is currently in a blocked state, all current elements will be restarted after execution (meaning the information received by the Task_done () Call of each object joining the queue) |
|
Join ()
|
Blocking: Waits for all tasks in the queue to finish executing. When a consumer thread calls Task_done (), the outstanding count in the queue is reduced until the count is 0 and unblocked.
|
|
Put (Item,block,timeout)
|
Put the object item into the queue: Item: Object name, required. Block The default is true if the queue is full waiting. Set to False if the queue is full to report an out-of-the-line exception. Timeout: "Block for true is in effect" The default is none if the queue is full waiting. 0: Don't wait, the queue is full immediately. Positive 1~: Wait for the corresponding number of seconds, the number of seconds to, the queue is full, error. |
|
Put_nowait (item) |
Save objects to the queue without waiting for |
|
Get (Block,timeout)
|
Remove the object from the queue and delete the object from the queue Block The default is true, and the queue is empty waiting. Can be changed to False if the queue is empty, reporting empty error. Timeout: "Block for true is in effect" The default is none, the queue is empty, wait. 0: Do not wait, the queue is empty directly to report empty. Positive 1~: Waits for the corresponding number of seconds, and if it is still empty, report empty |
|
Get_nowait () |
Fetching objects from the queue, not waiting for |
|
Qsize ()
|
Returns the approximate value of the queue length. The qsize length is not used as the basis for the get and put methods. |
|
Empty ()
|
Queue is empty returns True Do not act as a basis for get and put methods. |
|
Full ()
|
The queue is full returns true Do not act as a basis for get and put methods. |
|
Iv. Queue data Entry and exit rule examples:
is also one of the simplest examples of producer consumers.
"Example one: Queue basic entry and exit Rules ' ' Import queue,time,threading,randomdef productor (name,s): # producer function, put product time.sleep (s) print in the queue (' Waiter {} has time '. Format (name)) q.put (name) Def customer (): # consumer function, take product s from the queue = q.get () print (' waiter {} was called away '. Format (s)) L = []q = queue. Lifoqueue () # last in first out, change Lifoqueue to queue, FIFO. For i in range (5): N = random.randint (1,7) t = threading. Thread (target=productor,args= (i,n)) # production line L.append (t) t.start () for i in l:i.join () customer () #-----ShipmentLine result: Because there is random, so the result is not fixed, mainly observe the consumption sequence. ------Waiter 0 have time waiter 0 was called away Waiter 1 have time waiter 1 was called away Waiter 4 have time waiter 3 have time waiter 2 have time waiter 2 was called away Waiter 3 was called away Waiter 4 was called away
Resources:
http://python.jobbole.com/87592/
Https://www.2cto.com/kf/201608/540910.html
Https://www.cnblogs.com/itogo/p/5635629.html
[Python Module] Queue