标签:线程 water 分享图片 image lease 生产 ons his python版本
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()
标签:线程 water 分享图片 image lease 生产 ons his python版本
原文地址:http://blog.51cto.com/13718210/2134955