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)