Using Python multithreading to implement producer and consumer models

Source: Internet
Author: User
1, the Python version I used

2, the following to write the specific implementation process

Import threading
Import time
Import Queue

#首先生成一个队列
Q =queue.queue ()

#生产者
def producer (name):
L=threading. Rlock ()
For I in range (40):
L.acquire ()
Q.put (i)
L.release ()
Print "This is thead name was%s, produce num is%s"% (name,i)
Time.sleep (2)

#消费者
DEF consumer (name):
Count =0
While Count <=20:
Resulte =q.get ()
print ' The thread name is%s and the consume num is%s '% (Name,result)
Time.sleep (4)

#测试
For I in range (10):
p = Threading. Thread (target=producer,args= (' xxxx ',))
P.start ()

        c =threading.Thread(target=consumer,args=(‘yyyy‘,))        c.start()

Using Python multithreading to implement producer and consumer models

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.