python 自带线程池模块
python 自带线程池
from concurrent.futures import ThreadPoolExecutor, as_completed
import time
brand_list = ['xiaomi', 'apple', 'vivo', 'oppo', 'meizu', 'sanxing', 'huawei', 'zte', 'yijia']
res = []
def demo_task(index, brand, arg1):
print('===', index, brand, arg1)
return index, brand, arg1
start_time = time.time()
with ThreadPoolExecutor(max_workers=50) as pool:
task_list = []
# 创建任务,并执行
for index, brand in enumerate(brand_list):
task = pool.submit(demo_task, index, brand, 'arg1')
task_list.append(task)
# print(task.result()) # 千万不要在这里接收结果,会影响效率
# 接收任务结果
for future in as_completed(task_list):
data = future.result()
res.append(data)
print(f'{res}')
# 会在这里阻塞,主线程等待任务全部结果才继续走下去
print(time.time() - start_time)