python中的生产者以及消费者实现
#!/usr/bin/python# -*- coding: UTF-8 -*-import threadingimport timeclass Producer(threading.Thread):def run(self):global countwhile True:if con.acquire():
·
#!/usr/bin/python # -*- coding: UTF-8 -*- import threading import time class Producer(threading.Thread): def run(self): global count while True: if con.acquire(): if count > 1000: con.wait() else: count = count+100 msg = self.name+' produce 100, count=' + str(count) print msg con.notify() con.release() time.sleep(1) class Consumer(threading.Thread): def run(self): global count while True: if con.acquire(): if count < 100: con.wait() else: count = count-3 msg = self.name+' consume 3, count='+str(count) print msg con.notify() con.release() time.sleep(1) count = 500 con = threading.Condition() def test(): for i in range(2): p = Producer() p.start() for i in range(5): c = Consumer() c.start() if __name__ == '__main__': test()
更多推荐
已为社区贡献7条内容
所有评论(0)