- 首页 > it技术 > >
import multiprocessing #多进程模块def fun(q,number,lst): #自定义执行函数
arr = []
for i in lst:
arr.append(i)
q.put(arr)#将结果放入列队if __name__=='__main__':
lst = list(range(99))
thread_input_list = dict()
partition_point = int(len(lst)/4) #分4个进程,将lst分成4份
for thread_number in range(0,4):
thread_input_list[thread_number] = lst[(thread_number*partition_point):(thread_number+1)*partition_point]processes = dict()
queues = dict()
#分4个进程,每个进程有自己的lst
for thread in range(0,4):
queues[thread] = multiprocessing.Queue()
processes[thread] = multiprocessing.Process(target=fun,args=(queues[thread],thread,thread_input_list[thread]))
processes[thread].start()thread_position_list = dict()
#取回结果
for thread in range(0,4):
processes[thread].join()
thread_position_list[thread] = queues[thread].get()
#将结果输出
for i in range(0,4):
for value in thread_position_list[i]:
print(value)
推荐阅读