Python3进程通信之Queue

这篇博客展示了如何使用Python的multiprocessing模块进行数据处理。通过创建Process和Queue,实现数据的写入与读取。接着,介绍了如何使用Pool和Manager创建一个类MyPool,用于并行执行任务并将结果存入Queue。示例中,数据被分割并写入Queue,然后按顺序读取并显示结果。这些例子展示了Python中多进程和队列管理的基本用法。
摘要由CSDN通过智能技术生成

Process、Queue

# -*- encoding: utf-8 -*-
from multiprocessing import Process, Queue
import os, time, random


# 写数据进程执行的代码:
def write(q):
    print('Process to write: %s' % os.getpid())
    for value in ['A', 'B', 'C']:
        print('Put %s to queue...' % value)
        q.put(value)
        time.sleep(random.random())


# 读数据进程执行的代码:
def read(q):
    print('Process to read: %s' % os.getpid())
    while True:
        if q.empty():
            print('empty')
            break
        value = q.get(True)
        print('Get %s from queue.' % value)
        time.sleep(3)


if __name__ == '__main__':
    # 父进程创建Queue,并传给各个子进程:
    q = Queue()
    pw = Process(target=write, args=(q,))
    pr = Process(target=read, args=(q,))
    # 启动子进程pw,写入:
    pw.start()
    # 启动子进程pr,读取:
    pr.start()
    # 等待pw结束:
    pw.join()
    pr.join()
    # pr.terminate()  # 强行终止

Process to write: 16940
Put A to queue...
Process to read: 3424
Get A from queue.
Put B to queue...
Put C to queue...
Get B from queue.
Get C from queue.
empty

Process finished with exit code 0

Pool、Queue

# -*- encoding: utf-8 -*-
from multiprocessing import Pool, Manager
import os, time, random


class MyPool(object):
    def __init__(self, count):
        self.pool = Pool(count)
        self.queue = Manager().Queue()

    def start_task(self, task, datas, max_count=3):
        for i in range(0, len(datas), max_count):
            data = datas[i: i+max_count]
            self.pool.apply_async(task, args=(self.queue, data))
        self.pool.close()
        self.pool.join()

    def get_result(self):
        print('=================================')
        print('Get queue data...')
        while True:
            if self.queue.empty():
                print('empty')
                break
            value = self.queue.get()
            print('Get %s from queue.' % value)
        return True


def task(q, datas):
    '''
    写数据进程执行的代码
    '''
    print('Process to write: %s' % os.getpid())
    for value in datas:
        print('Put %s to queue...' % value)
        q.put(value**2)
        time.sleep(random.random())


def main():
    print('Parent process %s.' % os.getpid())
    datas = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    count = 3  # 进程数
    my_pool_obj = MyPool(count)
    my_pool_obj.start_task(task, datas)
    my_pool_obj.get_result()
    return True


if __name__ == '__main__':
    main()

Parent process 260.
Process to write: 14440
Put 7 to queue...
Process to write: 7644
Put 4 to queue...
Process to write: 7208
Put 1 to queue...
Put 2 to queue...
Put 5 to queue...
Put 8 to queue...
Put 3 to queue...
Put 6 to queue...
Process to write: 7208
Put 10 to queue...
Put 9 to queue...
=================================
Get queue data...
Get 49 from queue.
Get 16 from queue.
Get 1 from queue.
Get 4 from queue.
Get 25 from queue.
Get 64 from queue.
Get 9 from queue.
Get 36 from queue.
Get 100 from queue.
Get 81 from queue.
empty

Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值