Queue characteristics:FIFO--Advanced queue element first out queue. It comes from the queues in our lives (the first line is done).
The queue module most often forms the production-consumer model along with the threading module, which provides a first-in-one data structure for multithreaded programming, the queue.
The module contains 5 classes of source code:
Where empty and full are two exception classes, throwing Emptyexception exceptions if the queue is empty when the queue is Queue.get (block=0) or called get_nowait ().
Similarly, when the queue Queue.put (block=0) or put_nowait () is called, the fullexception exception is thrown if the queue is to reach maxsize.
Three other classes:
Queue class : A typical queuing model, FIFO first-in, first-out. The Class Queue.queue (maxsize) maxsize is the queue length, indicating the maximum number of data that can be stored in the queue. Once the upper limit is reached, insertion can cause 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.
Lifoqueue class : Inherits from the queue, similar to the stack, first in and out. Class Queue.lifoqueue (MaxSize)
Priorityqueue class : Inherited from the queue, priority queues, the lower the first step out. Class Queue.priorityqueue (MaxSize)
So, as long as the queue class, the basic deal with the queue module.
Common methods in the queue class:
Queue.qsize () returns the size of the queue
Queue.empty () returns True if the queue is empty, and vice versa false
Queue.full () returns True if the queue is full, otherwise false
Queue.full corresponds to maxsize size
Queue.get ([block[, timeout]]) Gets the queue, timeout wait time, and the Get () method of the call queue object is removed from the team header and returns an item. The optional parameter is block, which is true by default. If the queue is empty and the Block is True,get (), the calling thread is paused until a project is available. If the queue is empty and the block is false, the queue throws an empty exception.
queue.get_nowait () equivalent to queue.get (False)
Queue.put (item)
Write queue, timeout wait time, call the queue object's put () method to insert an item at the end of the team. Put () has two parameters, the first item is required, the value of the inserted item, the second block is an optional parameter, and the default is 1. If the queue is currently empty and the Block is the 1,put () method, the calling thread pauses until a data cell is vacated. If the block is the 0,put method, the full exception is thrown.
queue.put_nowait (item) quite Queue.put (item, False)
Queue.task_done ()
After completing a work, the Queue.task_done () function sends a signal to the queue that the task has completed
Queue.join ()
It actually means waiting until the queue is empty and then doing something else
A small practice example:
#coding: Utf-8import random, threading, Timeimport queue "" implements a production line to add a random number of 10 to the queue, implementing a consumer thread that consumes odd random numbers and even random numbers, respectively. Class producer (threading. Thread): Def __init__ (self,t_name,queue): Threading. thread.__init__ (self,name=t_name) self.data = Queue def run (self): For I in xrange: Random_num = Random.ra Ndint (1,99) print "%s:%s produced a random number \033[31;0m%d \033[0m put in queue"% (Time.ctime (), Self.getname (), Random_num) self.d Ata.put (Random_num) time.sleep (1) print "Production line complete!! "Class consumer (threading. Thread): Def __init__ (self,t_name,queue): Threading. thread.__init__ (self,name=t_name) self.data = Queue def run (self): while True:try:tmp_num = Self.data . Get (1,5) #定义超时时间5秒 if tmp_num%2 = = 0:print "%s:%s consumes an even number of random numbers in a queue \033[31;0m%d \033[0m"% (Time.ctime () , Self.getname (), Tmp_num) Time.sleep (2) else:print "%s:%s consumes an odd number of random numbers in a queue \033[31;0m%d \033[0m" % (Time.ctime (), Self.getname (), Tmp_num) Time.sleEP (2) except:print "Consumer thread complete!! "#一旦到达超时时间5秒, throws an exception, break exits the loop breakdef main (): queue = queue.queue (0) Pro = producer (' Pro ', queue) con = consumer (' Con ', queue) Pro.start () Con.start () Pro.join () con.join () print ' All threads complete!!! ' if __name__ = = ' __main__ ': Main ()
Thank you for reading, hope to help everyone, thank you for the support of this site!