首页 > 编程语言 > 详细

使用Python多线程实现生产者与消费者模型

时间:2018-07-02 11:42:33      阅读:230      评论:0      收藏:0      [点我收藏+]
1,我所使用到的python版本
技术分享图片

2,下面编写具体的实现过程

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 is %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()

使用Python多线程实现生产者与消费者模型

原文:http://blog.51cto.com/13718210/2134955

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!