题目:python起2个进程一个为生产者、一个为消费者(消费者进程中起协程消费)
from multiprocessing import Process, Queue
import time
import random
import gevent
# 消费者方法
def consumer(q):
while True:
time.sleep(2)
gevent_data_list = []
gevent_data_list.append(gevent.spawn(pop_data, q))
gevent.joinall(gevent_data_list)
def pop_data(q):
res = q.get()
print("消费了%s " % (res))
# 生产者方法
def producer(q):
# for i in range(10):
while True:
time.sleep(1)
number = random.randint(1, 10)
print("生产了 %s" % (number))
q.put(number)
if __name__ == "__main__":
# 创建队列
q = Queue()
# 创建生产者
p1 = Process(target=producer, args=(q,))
c1 = Process(target=consumer, args=(q,))
p1.start()
c1.start()