think more more

导航

python多进程代码示例

python多进程代码示例

  • 可将每个进程处理的结果汇总到一起
# !/usr/bin/env python
# -*- coding: utf-8 -*-

import time
import multiprocessing
from multiprocessing import Process,Lock,Manager

def worker(no,data,retdict):
    retdict[no] = (data[0],data[-1])
    time.sleep(2)
    print(f"worker no={no}, data range=[{data[0]},{data[-1]}]")

def multiple_process(data_list,process_count):
    manager = Manager()
    # 用于保存多进程处理结果,结果数据结构自己定义
    retdict = manager.dict()
    jobs = []
    data_count = len(data_list)
    step = int(data_count/process_count) + 1
    no = 0
    for i in range(0,data_count,step):
        data = data_list[i:min(i+step,data_count)]
        p = Process(target=worker,args=(no,data,retdict))
        no += 1
        jobs.append(p)
        p.start()
    for p in jobs:
        p.join()
    return retdict

if __name__ == '__main__':
    li = list(range(1,9))
    print(li)
    retdict = multiple_process(li,2)
    print(type(retdict))
    for n,r in retdict.items():
        print(n,r)

posted on 2021-08-07 09:01  duothink  阅读(149)  评论(0编辑  收藏  举报