python多线程2

from concurrent.futures import ThreadPoolExecutor
import threading

def list_of_groups(init_list, children_list_len):
'''拆分列表'''
    list_of_groups = zip(*(iter(init_list),) *children_list_len)
    end_list = [list(i) for i in list_of_groups]
    count = len(init_list) % children_list_len
    end_list.append(init_list[-count:]) if count !=0 else end_list
    return end_list

def action(data):
	for i in data:
		print(i)
		
l1 =  [i for i in range(1000)]
l2 = list_of_groups(l1,100) #

 with ThreadPoolExecutor(max_workers=20) as pool:
        # 使用线程执行map计算
        # 后面元组有10个元素,因此程序启动10条线程来执行action函数
        results = pool.map(action, l2)

你可能感兴趣的:(学习,总结,python)