队列通信

from multiprocessing import Process, Queue
import time,os


def w_fun(qunue_p):
    print('写进程号', os.getpid())

    time.sleep(2)
    for i in range(11):
        if qunue_p.full():
            print('队列已满,写进程正在等待。。。')
        # time.sleep(2)
        qunue_p.put(i)

    # qunue_p.put(11)
    # qunue_p.put(12)
    # qunue_p.put(13)
    # qunue_p.put(14)
    # qunue_p.put(15)
    # # 阻塞
    # qunue_p.put(16)


def do_fun(obj_p):
    print('进程号', os.getpid())
    print('处理中。。。', obj_p)


def r_fun(qunue_p):
    # task1 = qunue_p.get()
    # task2 = qunue_p.get()
    # task3 = qunue_p.get()
    # task4 = qunue_p.get()
    # task5 = qunue_p.get()
    # # task6 = qunue_p.get()
    # print(task1, task2, task3, task4, task5)

    print('读进程号', os.getpid())

    for i in range(11):
        if qunue_p.qsize() == 0:
            print('读进程陷入阻塞,请等待。。。')
        t = qunue_p.get()
        print('读到任务--->', t)
        # 穿件处理任务的进程
        pdo = Process(target=do_fun, args=(t,))
        pdo.start()
        pdo.join()



def main():
    q = Queue(10)

    pw = Process(target=w_fun, args=(q,))
    pw.start()
    # pw.join()
    pr = Process(target=r_fun, args=(q,))
    print('朱建成号', os.getpid())
    pr.start()
    pw.join()
    print('写进程结束!!!')
    pr.join()
    print('读进程结束!!!')
    print('finish')


if __name__ == '__main__':
    main()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值