美文网首页Python基础系列
Python标准模块_concurrent.futures

Python标准模块_concurrent.futures

作者: 我爱学python | 来源:发表于2019-07-25 16:03 被阅读12次

    介绍:

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

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

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

    基本方法:

    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)取得结果  相当于ret.get() 和ret.result()

    add_done_callback(fn)回调函数 callback = fn

    如何利用concurrent.futures模块开启线程池:

    使用map开启线程池

    回调函数:

    相关文章

      网友评论

        本文标题:Python标准模块_concurrent.futures

        本文链接:https://www.haomeiwen.com/subject/xnvhrctx.html