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

 

转载于:https://www.cnblogs.com/wt7018/p/11062478.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值