python学习笔记(63) 线程池

submit(fn,*args,**kwargs)  # 异步提交任务

map(fn,iterable)  # 拿不到返回值

shutdown()  # 相当于pool.close()加pool.join() 操作

result()  # 取得结果

add_done_callback()  # 回调函数

import time
from concurrent.futures import ThreadPoolExecutor

def func(n):
time.sleep(2)
print(n)
return n*n

def callback(m):
print('结果是%s'%m.result())

tpool = ThreadPoolExecutor(max_workers=5)
t_lst = []
for i in range(20):
t = tpool(func,i).add_done_callback(callback)
t_lst.append(t)

tpool.shutdown()
print('主线程')
for i in t_lst:
print('***'+i.result())

猜你喜欢

转载自www.cnblogs.com/farion/p/10031037.html
今日推荐