Python thread synchronization: production/Consumer mode

Source: Internet
Author: User

The queue object in Python provides support for thread synchronization by using the Queue object to enable FIFO-first-out queues that are formed by multi-producer and multi-consumer.

Each producer puts data in a queue, and each consumer pulls data from the queue in turn.

# coding:utf-8import threading,time,queueclass producer (Threading. Thread):     def __init__ (self,threadname):         threading. thread.__init__ (Self,name=threadname)     def run (self):         global queue        queue.put (Self.getName ())         print  self.getname (), ' Put ', self.getName (), ' to  Queue ' Class consumer (threading. Thread):     def __init__ (self,threadname):         threading. thread.__init__ (Self,name=threadname)     def run (self,threadname):         global queue        print  Self.getname (), ' Get ', Queue.get (), ' From queue ' #生成队列对象queue  = queue.queue () #生List of production objects plist = [] #消费者对象列表clist  = []for i in range (Ten):     p = producer (' Producer '  + str (i))     plist.append (p) for i  in range:     c = consumer (' Consumer '  + str (i)) #运行生产者for  i in plist:    i.start ()     i.join () #运行消费者for  i  in clist:    i.start ()     i.join ()

Operation Result:

Producer0 put Producer0 to QueueProducer1 put Producer1 to QueueProducer2 put Producer2 to QueueProducer3 put Producer3 to QueueProducer4 put Producer4 to QueueProducer5 put Producer5-QueueProducer6 put Producer6 to QueueProducer7 put Produc Er7 to QueueProducer8 put Producer8-QueueProducer9 put Producer9 to queueprocess finished with exit code 0

In addition, it can be implemented using Stackless python, which is just a modified version of Python and has better support for multithreaded programming. If you have high requirements for multithreaded applications, you might consider using stackless python to do this.

Stackless Official website: https://bitbucket.org/stackless-dev/stackless/wiki/Home

The same implementation of the producer consumer case code is as follows:

Import Stackless,queuedef Producer (i): Global queue queue.put (i) print "Producer", I, ' Add ', Idef Consumer (): Glo Bal Queue i = queue.get () print ' Consumer ', I, ' get ', Iqueue = Queue.queue () for I in range: Stackless.tasklet ( Producer) (i) for I in range: Stackless.tasklet (Consumer) () Stackless.run ()

Execution Result:

Producer 0 Add 0Producer 1 Add 1Producer 2 Add 2Producer 3 Add 3Producer 4 Add 4Producer 5 Add 5Producer 6 Add 6Producer 7 Add 7Producer 8 Add 8Producer 9 add 9Consumer 0 get 0Consumer 1 get 1Consumer 2 get 2Consumer 3 get 3Consumer 4 get 4Cons Umer 5 Get 5Consumer 6 get 6Consumer 7 get 7Consumer 8 get 8Consumer 9 get 9Process finished with exit code 0

Stackless Python provides support for micro-threading, which is a lightweight thread that consumes less resources than the previous thread.

This article is from the "-= lake-side Bamboo =-" blog, please be sure to keep this source http://bronte.blog.51cto.com/2418552/1872549

Python thread synchronization: production/Consumer mode

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.