multiprocessing.Queue
- q = multiprocessing.Queue()
- q.put(x) # 向队列中放入一个值
- q.get() # 从队列中取一个值
- q.empty() # 判断队列是否为空
- q.full() # 判断队列是否满了
- q.get_nowait() # 从队列中取值,如果队列为空,则抛出异常。普通get()方法为空时堵塞
- q.qsize() # 求取队列长度
生产者消费者模型
用来解决数据供需的平衡问题
- 生产者 -- 进程
- 消费者 -- 进程
import multiprocessing
import time
import random
def consumer(q, name):
while True:
food = q.get()
if food is None:
print("%s 拿到一个空" % name)
break
print("\033[31m%s 消费了 %s\033[0m" %(name, food))
time.sleep(random.randint(1, 3))
def producer(name, food, q):
for i in range(10):
f = "%s生产的 %s %s" % (name, food, i)
print(f)
q.put(f)
time.sleep(1)
if __name__ == "__main__":
q = multiprocessing.Queue(20)
p = multiprocessing.Process(target=producer, args=("Sun", "包子", q))
p.start()
p2 = multiprocessing.Process(target=producer, args=("Wang", "泔水", q))
p2.start()
c1 = multiprocessing.Process(target=consumer, args=(q, "1号"))
c1.start()
c2 = multiprocessing.Process(target=consumer, args=(q, "2号"))
c2.start()
p.join()
p2.join()
q.put(None)
q.put(None)
JoinableQueue
使用阻塞方式等待队列空闲后退出
import multiprocessing
import time
import random
def consumer(q, name):
while True:
food = q.get()
print("\033[31m%s 消费了 %s\033[0m" %(name, food))
time.sleep(random.randint(1, 3))
q.task_done()
def producer(name, food, q):
for i in range(10):
f = "%s生产的 %s %s" % (name, food, i)
print(f)
q.put(f)
time.sleep(1)
q.join()
if __name__ == "__main__":
q = multiprocessing.JoinableQueue(20)
p1 = multiprocessing.Process(target=producer, args=("Sun", "包子", q))
p1.start()
p2 = multiprocessing.Process(target=producer, args=("Wang", "泔水", q))
p2.start()
c1 = multiprocessing.Process(target=consumer, args=(q, "1号"))
c2 = multiprocessing.Process(target=consumer, args=(q, "2号"))
c1.daemon = True
c2.daemon = True
c1.start()
c2.start()
p1.join()
p2.join()
网友评论