Queue characteristics: Advanced First Out (FIFO)--The elements of the advanced queue are first out of the queue. Come from the queues in our Lives (queue first and do it first).
The queue module most often forms a production-consumer model together with the threading module, providing a first-in, first-out data structure for multithreaded programming, i.e. queues.
This module contains 5 classes in the source code:
Where empty and full are two exception classes, when the queue Queue.get (block=0) or calls get_nowait (), if the queue is empty, the emptyexception exception is thrown.
Similarly, when the queue is Queue.put (block=0) or when the put_nowait () is invoked, the Fullexception exception is thrown if the queue reaches maxsize.
Other three classes:
Queue class : A typical queuing model, FIFO first in first. Class Queue.queue (MaxSize) maxsize is the queue length, indicating the upper limit of the number of data that can be stored in the queue. Once the cap is reached, the insertion will cause a blockage UNFICYP
Until the data in the queue is consumed. If the maxsize is less than or equal to 0, the queue size is not limited.
Lifoqueue class : Inherits from queue, similar to stack, first in and out. Class Queue.lifoqueue (MaxSize)
Priorityqueue class : Inherit from queue, priority queue, the lower the level the more first out. Class Queue.priorityqueue (MaxSize)
So, as long as the queue class, you can basically handle 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, false instead
Queue.full () returns True if the queue is full, false instead
Queue.full and maxsize size correspond
Queue.get ([block[, timeout]]) Gets the queue, timeout the wait time, and the Get () method of the calling queue object deletes from the team header and returns an item. The optional argument is block, and the default is true. 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 a empty exception.
queue.get_nowait () is equivalent to queue.get (False)
Queue.put (item)
Write queue, timeout wait time, the put () method of the calling queue object inserts a project 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-1,put () method suspends the calling thread until a data unit is empty. If the block is a 0,put method, a 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 ()
Actually means wait until the queue is empty, and then do something else.
A small example of practice:
#coding: Utf-8 import Random, threading, time import queue "implements a production line that adds 10 random numbers to the queue, implements a consumer thread, consumes odd and even random numbers ' cl Ass 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 (a): Random_num = Rando
M.randint (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.data.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 even random number \033[31;0m%d \033[0m"% (time) in a queue. CTime (), Self.getname (), Tmp_num) Time.sleep (2) else:print "%s:%s consumes odd random number in one 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 break def main (): queue = queue.queue (0) Pro = producer (' Pro ', queue) con = c
Onsumer (' Con ', queue) Pro.start () Con.start () Pro.join () con.join () print ' All threads complete!!! ' if __name__ = = ' __main__ ': Main ()
Thank you for reading, I hope to help you, thank you for your support for this site!