Python multithreaded programming-queue module and producer-consumer issues

Source: Internet
Author: User

Excerpt from Python core programming

In this example, the producer-consumer model: the producer of a commodity or service produces a commodity and then puts it into a queue-like data structure. The time in the production of goods is uncertain, and the time for consumers to consume goods is also uncertain.

Use the queue module (called queue in the python2.x version) to provide a mechanism for inter-thread communication so that data can be shared between threads. Specifically, you create a queue where the producer (thread) puts the new product, and the consumer (thread) consumes the goods.

The following table is a partial property of the queue module:

Property Describe
Class of the queue module
Queue (maxsize=0) Create a first in, first out queue. If the maximum value is given, it is blocked when the queue has no space, otherwise, an infinite queue
Lifoqueue (maxsize=0) Creates a post-in, first-out queue. If the maximum value is given, it is blocked when there is no space in the queue; otherwise, an infinite sequence
Priorityqueue (maxsize=0) Create a priority queue. If the maximum value is given, it is blocked when there is no space in the queue; otherwise, an infinite sequence
Queue exception
Empty Throws an exception when calling the Get* () method on an empty queue
Full An exception is thrown when the put* () method is called on a full queue
Queue Object Methods
Qsize () Returns the queue size. (This value is approximate because it may be modified by another thread when the queue size is returned)
Empty () Returns true if the queue is empty, otherwise false
Full () Returns true if the queue is full, otherwise false
Put (Item,block=true,timeout=none) Put the item in the queue. If the block is true (the default) and timeout is none, it is blocked before there is free space, and if timeout is positive, the timeout second is blocked, and if block is false, an empty exception is thrown
Put_nowait (item) Same as put (item,false) effect
Get (Block=true,timeout=none) Gets the element from the queue. If a block (not 0) is given, it blocks until an element is available
Get_nowait () and get (False) effect to use
Task_done () Used to indicate that an element in the queue has been completed and the method is used by the following join ()
Join () remains blocked until all elements in the queue have finished executing and call the above task_done () signal.

The following prodcons.py script uses the queue object to implement producer-consumer scenarios, randomly producing or consuming goods, and producer and consumer independent, concurrent threads of execution. Note that the Mythread class that was rewritten in the previous section is used here.

#Python 3.6 fromRandomImportRandint fromTimeImportSleep fromQueueImportQueue fromMyThreadImportMyThread#put an object in the queuedefWriteq (queue):Print('producing for queue .....') Queue.put ('Product', 1)    Print('Current number of products:', Queue.qsize ())#an object in the consumption queuedefREADQ (Queue): Val= Queue.get (1)    Print('consuming goods from the queue ... Remaining items after consumption:', Queue.qsize ())#imitate the producer. defwriter (queue,loops): forIinchRange (Loops): Writeq (queue) sleep (Randint (1, 3))#Writer's sleep time is generally shorter than reader, in order to prevent reader from getting objects from an empty queue, in other words, when the turn to reader execution, there is a greater likelihood that a consumable object exists. #imitate the consumerdefReader (queue,loops): forIinchRange (Loops): READQ (queue) sleep (Randint (2,5)) Funcs=[Writer,reader]nfuncs=Range (len (funcs))defMain (): Nloops= Randint (2,5)#Randint and Randrange are similar, the difference is that Randrange is half open half-closed interval, and Randint is closed intervalQ = Queue (32) Threads= []#Analog thread pool     forIinchNfuncs:t= MyThread (Funcs[i], (Q,nloops), Funcs[i].__name__)#Creating Threadsthreads.append (t) forIinchNfuncs:threads[i].start ()#Start thread Execution             forIinchNfuncs:threads[i].join ()Print('End')    if __name__=='__main__': Main ()

The execution effect is similar:

PS c:\users\wc> python E:\Python3.6.3\workspace\prodcons.py began executing writer  21:06:22 2018 is producing for the queue ... ... Begin the execution  of reader21:06:22201811112 19 21:06:30 2018  121:06:39 2018 End

Python multithreaded programming-queue module and producer-consumer issues

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.