python provides a queue module to specifically implement Message Queuing queue objects
The queue object implements a FIFO queue (the other is the LIFO, priority queue, which is no longer described here).
Queue only MaxSize A constructor parameter that specifies the capacity of the queue, which represents an unlimited capacity when specified as 0.
There are mainly the following member functions:
Queue.qsize (): Returns the current space of the message queue. The returned value is not necessarily reliable.
Queue.empty (): Determines whether the message queue is empty and returns True or false. is equally unreliable.
Queue.not_empty (): Determines whether the message queue is non-empty. Ditto is unreliable.
Queue.full (): Similar to the top, to determine whether the message queue is full.
Queue.put (item, Block=true, Timeout=none): Stores messages in message queues. Blocks can control whether the block is blocked, and timeout specifies the wait time when blocking. If it is not blocked or timed out, it will cause a full exception.
Queue.put_nowait (item): Equivalent to put (item, False).
Queue.get (Block=true, Timeout=none): Gets a message with the other put.
Queue.task_done (): The thread that receives the message calls this function to indicate that the task that corresponds to the message is complete.
Queue.join (): Indicates wait, wait until the queue is empty, and perform other operations.
Queue.terminate (): Indicates forced shutdown.
Example:
#!/usr/bin/env python# -*- coding:utf-8 -*-from multiprocessing import Queuefrom threading import threadimport time "" writes a consumer and producer, implemented in a multi-threaded manner, by means of a class rewrite. "" "Class proceducer (Thread): def __init__ (Self, queue): super (proceducer,self). __init__ () self.queue = queue def run (self): try: for i in xrange (1, 10): print ("Put data is {0} to queue". Format (i)) self.queue.put (i) excePt exception as e: print ("Put data error") Raise eclass consumer_even (Thread): def __init__ (Self, queue): super (consumer_even , self). __init__ () self.queue = queue def run (self): try: while not self.queue.empty (): number = self.queue.get (block=True, timeout=3) if number % 2 != 0: print ("Get {0} from queue even, thread name is {1}". Format ( Number, self.getname ())) else: self.queue.put (number) time.sleep (1) except exception as e: Raise eclass consumer_odd (Thread): def __init__ (Self, queue): super (consumer_odd , self). __init__ () Self.queue = queue def run (self): try: while not Self.queue.empty (): number = self.queue.get (block=true, timeout=3) if number % 2 == 0: Print ("get {0} from queue odd". Format (number)) else: self.queue.put (number) &nbsP; time.sleep (1) except Exception as e: raise edef main (): queue = queue () p = proceducer (Queue=queue) p.start () p.join () time.sleep (1) c1 = consumer_even (queue=queue) c2 = consumer_odd (Queue=queue) c1.start () c2.start () c1.join () c2.join () print ("all thread terminate!") if __name__ == ' __main__ ': main ()
Results:
Put data is 1 to queueput data is 2 to queueput data is 3 to queueput data is 4 to queueput data is 5 to queueput data is 6 to queueput data is 7 to Queueput data is 8 to queueput data is 9 to queueget 1 from queue even, thread name is thread-2get 2 from queue oddget 3 from queue even, thread name is thread-2get 4 from queue ODDget 5 from queue EVEN, thread name is thread-2get 6 from queue oddget 7 from queue even, thread Name is thread-2get 8 from queue oddget 9 from queue even, thread name is  thread-2all thread terminate!
A. Python Queue module