from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
import time
def func(i):
print('star', i)
time.sleep(2)
return f'over,{i}'
def cb(res_obj):
print('callback=====>',res_obj) #callback=====> <Future at 0x21d7eb8 state=finished returned str>
print('ccc===>',res_obj.result()) #ccc===> over,0
pool=ThreadPoolExecutor(3) #(os.cpu_count() or 1) * 5,默认为当前CPU个数的5倍
#pool=ProcessPoolExecutor() #os.cpu_count() or 1 ,默认为当前CPU的个数
for i in range(1):
res=pool.submit(func, i)
print(res) #<Future at 0x21d7eb8 state=running>
res.add_done_callback(cb)
#pool.submit(func,i).add_done_callback(cb)