from datetime import datetime
from time import sleep
from concurrent.futures import ProcessPoolExecutor
# from helper.time_helper import calc_time
# @calc_time
def func1(name='xiaohong', age=18):
print(u'方法已被调用')
sleep(2)
print(f'{name}今年{age}岁...')
return name
if __name__ == '__main__':
s_date = datetime.now()
# 构造任务参数列表
tasks = [['xiaohong', 12], ['xiaohuang', 13], ['xiaolv', 14]]
with ProcessPoolExecutor(max_workers=5) as pool:
# 如果需要传参,直接将参数列表放在pool.submit()方法第二、三个参数,往后排就好,submit接收多个参数,*args和**kwargs
results = [pool.submit(func1, tasks[i][0], tasks[i][1]) for i in range(len(tasks))]
# print(results)
# print(type(results))
# print(help(results))
for res in results:
# 获取多任务执行的函数的返回结果
print(res.result())
print(res.done())
f_date = datetime.now()
print(f'多任务耗时:{f_date - s_date}')
简单明了,一看就懂