from concurrent.futures import ThreadPoolExecutor
from time import sleep
tpool = ThreadPoolExecutor(max_workers=5) #设置线程最大数量
def func(i):
sleep(1)
print(i)
for i in range(20):
tpool.submit(func, i)
tpool.shutdown() #等待所有子线程运行结束
print('主线程在这里')
t.result() 获取函数的返回值
进程池concurrent.futures.ProcessPoolExecutor
from concurrent.futures import ProcessPoolExecutor
from time import sleep
tpool = ProcessPoolExecutor(max_workers=5) #这是最大进程数
def func(i):
sleep(1)
print(i)
for i in range(20):
tpool.submit(func, i)
tpool.shutdown() #等待所有子进程运行结束
print('主线程在这里')