>>Manager类共享数据操作
# 使用Manager 结合进程池实现生产者消费模式
import time
from multiprocessing import Queue, Process, Pool,Manager
def producter(q, data):
q.put(data)
def producter_1(q,data1):
q.put(data1)
def costermer(q):
while q.qsize() >0:
coster_data=q.get()
print('消费者1数据:%s'%coster_data)
if q.full():
time.sleep(5)
def costermer_1(q):
while q.qsize() >0:
coster_data=q.get()
print('消费者2数据:%s'%coster_data)
if q.full():
time.sleep(5)
def main():
manager =Manager() # 获取manager对象实例 共享进程
queue=manager.Queue(maxsize=10)
pool=Pool(3)
data = '112n8asdasjfasdasfasdasfasda'
data1= '12'
for i in data:
for j in data1:
# p1=Process(target=producter,args=(queue,i))
# p2=Process(target=costermer,args=(queue,))
# p3 = Process(target=producter_1,args=(queue,data1))
# p4 = Process(target=costermer_1,args=(queue,))
# p1.start()
# p2.start()
# p3.start()
# p4.start()
# p1.join()
# p2.join()
# p3.join()
# p4.join()
p1=pool.apply_async(func=producter,args=(queue,i))
p2=pool.apply_async(func=producter_1,args=(queue,j))
p3=pool.apply_async(func=costermer,args=(queue,))
p4=pool.apply_async(func=costermer_1,args=(queue,))
pool.close()
pool.join()
if __name__ == '__main__':
main()