数据不安全,不常用
import time from multiprocessing import Pipe, Process def producer(prod, cons, name, food): cons.close() for i in range(10): f = '%s生产%s%s' % (name, food, i) prod.send(f) time.sleep(0.5) print(f) prod.close() def consumer(prod, cons, name): prod.close() while 1: food = cons.recv() f = '%s吃了%s' % (name, food) time.sleep(0.7) print(f) if __name__ == '__main__': prod, cons = Pipe() p = Process(target=producer, args=(prod, cons, 'tom', '包子')) p.start() c = Process(target=consumer, args=(prod, cons, 'joker')) c.start() p.join() c.join()
本文通过Python的multiprocessing模块实现生产者消费者模式,演示了如何使用Pipe进行进程间通信,以及Process启动多个进程来模拟生产包子和消费包子的过程。
1686

被折叠的 条评论
为什么被折叠?



