python-多线程

 1 import sys
 2 import time
 3 import threading
 4 import queue
 5 
 6 def producer():
 7     for x in range(20):
 8         time.sleep(1)
 9         q.put(x)
10         print("生产%d个苹果" % x)
11 def cost():
12     for x in range(20):
13         time.sleep(1.5)
14         print("消耗%d个苹果" % q.get())
15 
16 if __name__ == '__main__':
17     q = queue.Queue(maxsize=5)
18     t1 = threading.Thread(target=producer)
19     t2 = threading.Thread(target=cost)
20     t1.start()
21     t2.start()

 

上一篇:Kafka核心技术与实战——14 | 幂等生产者和事务生产者是一回事吗?


下一篇:Kafka事务特性详解