submit | 异步提交 | |
map(func, *iterables, timeout=None, chunksize=1) | 类似 map(func, *iterables)除了以下: 迭代器是立即收集的而不是延迟收集的 func是异步执行的和对func的多个调用可以并发执行 | |
shutdown(wait=True) | 相当于进程池的pool.close()+pool.join()操作 wait=True,等待池内所有任务执行完毕回收完资源后才继续 wait=False,立即返回,并不会等待池内的任务执行完毕 但不管wait参数为何值,整个程序都会等到所有任务执行完毕 submit和map必须在shutdown之前 | |
result(timeout=None) | 取得结果,如果在for中引用将会形成阻塞 | |
add_done_callback(fn) | 回调函数,异步执行 |
submit 使用和验证异步运行:
import time
import os
import threading
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
def func():
time.sleep(2)
print('11',threading.current_thread().getName())
if __name__ == '__main__':
tpool = ThreadPoolExecutor(max_workers=2) #max_workers指定多少个线程
# tpool = ProcessPoolExecutor(max_workers=5) #进程池的使用只需要将上面的ThreadPoolExecutor改为ProcessPoolExecutor就行了,其他都不用改
start = time.time()
for i in range(2):
tpool.submit(func)
tpool.shutdown() #加了shutdown等close()加join(),锁pool并等子线程结束才主线程才结束
end = time.time() - start
print(end)
结果:
11 ThreadPoolExecutor-0_0
11 ThreadPoolExecutor-0_1
2.001002073287964
func()设定了休眠两秒,这时设置进程池,并用submit()功能运行,发现运行两次func()的时间是两秒,所以得出submit是异步运行结果
map的使用
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
import threading
import os,time,random
def task(n):
print('%s is runing' %threading.get_ident())
time.sleep(random.randint(1,3))
return n**2
if __name__ == '__main__':
executor=ThreadPoolExecutor(max_workers=3)
# for i in range(11):
# future=executor.submit(task,i)
s = executor.map(task,range(1,5)) #map取代了for+submit,将1,2,3,4的值传入task进行异步执行
print([i for i in s])
结果:
123145491988480 is runing
123145497243648 is runing
123145502498816 is runing
123145497243648 is runing
[1, 4, 9, 16]
回调函数(add_done_callback):
import time
import os
import threading
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
def func(n):
time.sleep(2)
return n*n
def call_back(m):
print('结果为:%s'%(m.result()))
tpool = ThreadPoolExecutor(max_workers=5)
t_lst = []
for i in range(5):
t = tpool.submit(func,i).add_done_callback(call_back) #将tpool.submit(func,i)的返回值给函数call_back异步执行
结果:
结果为:0
结果为:9
结果为:4
结果为:1
结果为:16
for中result引发阻塞
import time
from concurrent.futures import ThreadPoolExecutor
def func(n):
time.sleep(2)
return n*n
tpool = ThreadPoolExecutor(max_workers=5)
t_lst = []
start = time.time()
for i in range(5):
t = tpool.submit(func,i)
print(t.result())
end = time.time() - start
print("时间:%f"%end)
结果:
0
1
4
9
16
时间:10.023604