使用生产者和消费者的例子来介绍python的多线程
代码如下:
#生产者和消费者问题
from threading import Thread
from queue import Queue
q = Queue()
def producer():
global q
for i in range(10, 100):
q.put(i)
def consumer():
global q
for i in range(0,90):
con = q.get(True)
print(con)
pro_thread = Thread(target = producer)
cons_thread = Thread(target = consumer)
pro_thread.start()
cons_thread.start()
pro_thread.join()
cons_thread.join()