首页 > 编程语言 > 详细

线程池回调函数实时调用返回值

时间:2019-12-13 16:34:43      阅读:112      评论:0      收藏:0      [点我收藏+]
import time
from concurrent.futures import ThreadPoolExecutor  # 导入线程池模块

thread_pool = ThreadPoolExecutor(5)  # 设置线程池大小


def main(num):
    return f"这是第 {num}"


def start():
    for num in range(1000000):
        thread_object = thread_pool.submit(main, num)  # 参数为要执行的函数和所传参数
        thread_object.add_done_callback(parse)   # 把线程结果传递给parse函数


def parse(obj):
    print(obj.result())  # 通过result方法获取值


if __name__ == ‘__main__‘:
    now = time.time()
    start()
    print(f"总共耗时: {time.time() - now}")

  

线程池回调函数实时调用返回值

原文:https://www.cnblogs.com/wangtaobiu/p/12035891.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!