1、Python标准模块——concurrent.futures

concurrent.futures模块提供了高度封装的异步调用接口

ThreadPoolExecutor:线程池,提供异步调用

ProcessPoolExecutor:进程池,提供异步调用

2、基本方法

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

  map(func, *iterables, timeout=None, chunksize=1):取代for循环submit的操作

  shutdown(wait=True):相当于进程池的pool.close()+pool.join()操作

  wait=True,等待池内所有任务执行完毕回收完资源后才继续
  wait=False,立即返回,并不会等待池内的任务执行完毕
  但不管wait参数为何值,整个程序都会等到所有任务执行完毕
  submit和map必须在shutdown之前
  result(timeout=None):取得结果

  add_done_callback(fn):回调函数

  done():判断某一个线程是否完成

  cancle():取消某个任务

3、进程池:ProcessPoolExecutor

    from concurrent.futures import ProcessPoolExecutor
    import os,time,random
    def task(n):
        print('%s is runing' %os.getpid())
        time.sleep(random.randint(1,3))
        return n**2

    if __name__ == '__main__':

        executor=ProcessPoolExecutor(max_workers=3)

        futures=[]
        for i in range(11):
            future=executor.submit(task,i)
            futures.append(future)
        executor.shutdown(True)
        print('+++>')
        for future in futures:
            print(future.result())

4、线程池:ThreadPoolExecutor与进程池方法一样,参考进程池代码
5、map的用法:map取代了for+submit

from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
import os,time,random
def task(n):
    print('%s is runing' %os.getpid())
    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)

    executor.map(task,range(1,12)) #map取代了for+submit