python 进程管道

数据不安全,不常用

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()

 

posted @ 2019-06-21 08:20  市丸银  阅读(304)  评论(0)    收藏  举报