concurrent.futures模块

1.concurrent.futures模块介绍

2.ThreadPoolExecutor线程池使用

3.ProcessPoolExecutor进程池使用

1.concurrent.futures模块介绍

 1 # 介绍
 2     concurrent.futures模块提供了高度封装的异步调用接口
 3     ThreadPoolExecutor:线程池,提供异步调用
 4     ProcessPoolExecutor: 进程池,提供异步调用
 5 
 6 # 基本方法
 7 # submit(fn, *args, **kwargs)  异步提交任务
 8 
 9 # map(func, *iterables, timeout=None, chunksize=1)  取代for循环submit的操作
10 
11 # shutdown(wait=True) 相当于进程池的pool.close()+pool.join()操作
12     wait=True,等待池内所有任务执行完毕回收完资源后才继续
13     wait=False,立即返回,并不会等待池内的任务执行完毕
14     但不管wait参数为何值,整个程序都会等到所有任务执行完毕
15     submit和map必须在shutdown之前
16 
17 # result(timeout=None) 取得结果
18 
19 # add_done_callback(fn) 回调函数

2.ThreadPoolExecutor线程池使用

3.ProcessPoolExecutor进程池使用

猜你喜欢

转载自www.cnblogs.com/wyb666/p/9772868.html