Python queue details and instance code, python queue
Queue features:First-in-first-out (FIFO)-elements of the first-out queue. It comes from the queue in our life (queuing first and finishing first ).
The Queue module most often forms a production-consumer model together with the threading module, providing a first-in-first-out data structure suitable for multi-thread programming, that is, a Queue.
The module source code contains five classes:
Empty and Full are two exception classes. When Queue. get (block = 0) or get_nowait () is called, if the Queue is Empty, an EmptyException is thrown.
Similarly, when the Queue. put (block = 0) or put_nowait () is called, if the Queue reaches maxsize, A FullException exception is thrown.
The other three categories:
Queue class: A typical queue model, FIFO first-in-first-out. Class Queue. Queue (maxsize) maxsize indicates the Queue length and specifies the maximum number of data in the Queue. Once the upper limit is reached, insertion will cause congestion,
Until the data in the queue is consumed. If maxsize is smaller than or equal to 0, there is no limit on the queue size.
LifoQueue class: Inherited from the Queue, similar to the stack, first in and then out. Class Queue. LifoQueue (maxsize)
PriorityQueue class: Inherited from Queue, priority Queue. The lower the level, the more advanced. Class Queue. PriorityQueue (maxsize)
Therefore, as long as the Queue class is completed, the Queue module is basically completed.
Common methods in the Queue class:
Queue. qsize () returns the size of the Queue
Queue. empty () if the Queue is empty, True is returned. Otherwise, False is returned.
Queue. full () if the Queue is full, True is returned. Otherwise, False is returned.
Queue. full corresponds to maxsize
Queue. get ([block [, timeout]) gets the Queue and timeout wait time. Call the get () method of the Queue object to delete the Queue header and return a project. The optional parameter is block. The default value is True. If the queue is empty and the block is True, get () will suspend the calling thread until a project is available. If the queue is Empty and the block is False, the queue will cause an Empty exception.
Queue. get_nowait () is equivalent to Queue. get (False)
Queue. put (item)
Write queue, timeout wait time, call the put () method of the queue object to insert a project at the end of the team. Put () has two parameters. The first item is required and is the value of the inserted project. The second block is an optional parameter. The default value is 1. If the queue is empty and the block is 1, The put () method suspends the calling thread until a data unit is empty. If the block is 0, the put method will cause a Full exception.
Queue. put_nowait (item) is equivalent to Queue. put (item, False)
Queue. task_done ()
After a task is completed, the Queue. task_done () function sends a signal to the completed Queue of the task.
Queue. join ()
It actually means that when the queue is empty, other operations will be performed.
A small exercise example:
# Coding: utf-8import random, threading, timeimport Queue ''' implements a production thread, used to add 10 random numbers to the Queue, implements a consumption thread, consumption of odd and even random numbers ''' 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 (10): random_num = random. randint () print "% s: % s produces a random number \ 033 [31; 0 m % d \ 033 [0 m into the queue" % (time. ctime (), self. getName (), random_nu M) self. data. put (random_num) time. sleep (1) print "production thread completed !! "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 () # defines the timeout time for 5 seconds if tmp_num % 2 = 0: print "% s: % s consumes an even random number in a queue \ 033 [31; 0 m % d \ 033 [0 m "% (time. ctime (), self. getName (), tmp_num) time. sleep (2) else: print "% s: % s consumes an odd random number in a queue \ 033 [31; 0 m % d \ 033 [0 m" % (time. ctime (), Self. getName (), tmp_num) time. sleep (2) consume T: print "Consumption thread completed !! "# Once the timeout time reaches 5 seconds, an exception is thrown. 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 this article. I hope it will help you. Thank you for your support for this site!