zoukankan      html  css  js  c++  java
  • (19)ThreadPoolExecutor线程池

    # 线程池

    # 实例化线程池 ThreadPoolExcutor (推荐cpu_count*(n+1))

    # 异步提交任务 submit / map

    # 阻塞直到任务完成 shutdown

    # 获取子线程的返回值 result

    # 使用回调函数 add_done_callback

    (1)基本用法:

    from concurrent.futures import ThreadPoolExecutor
    
    def func(i):
    
        print("thread is start",i)
    
        print("thread is end ")
    
    if __name__ == "__main__":
    
        p = ThreadPoolExecutor(5)
    
        p.submit(func,1)  #启动线程
    
        p.shutdown()  # 相当于join+close
    
    print("主线程")
    View Code

    执行结果:

    thread is start 1
    thread is end 
    主线程
    View Code

    (2)返回值 ( 通过对象.result()拿到结果 )

    from concurrent.futures import ThreadPoolExecutor
    def func(i):
        print("thread %s start" % (i))
        print("thread %s end" % (i))
        return i * "*"
    tp = ThreadPoolExecutor(5)
    lst = []
    for i in range(20):
        res = tp.submit(func,i) #返回值也是对象
        lst.append(res)
    tp.shutdown()
    for res in lst:
        print(res.result())
    View Code

    执行结果:

    thread 0 start
    thread 0 end
    thread 1 start
    thread 2 start
    thread 1 endthread 2 end
    
    thread 3 start
    thread 3 end
    thread 4 start
    thread 4 end
    thread 5 startthread 6 startthread 7 start
    thread 7 end
    thread 8 start
    thread 8 end
    
    thread 9 start
    
    thread 6 end
    thread 10 start
    thread 10 end
    thread 11 start
    thread 11 end
    thread 12 start
    thread 12 end
    thread 13 start
    thread 13 end
    thread 14 start
    thread 14 end
    thread 15 startthread 9 endthread 5 end
    thread 16 start
    thread 16 end
    thread 17 start
    thread 15 end
    thread 18 start
    thread 18 end
    thread 19 start
    thread 17 end
    
    thread 19 end
    
    
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********
    ***********
    ************
    *************
    **************
    ***************
    ****************
    *****************
    ******************
    *******************
    主线程
    View Code

    (3)map 返回生成器

    from concurrent.futures import ThreadPoolExecutor
    from threading import current_thread as ct
    def func(i):
        print("thread",i,ct().ident)
        print("thread %s end" % (i))
        return i * "*"
    tp = ThreadPoolExecutor(5)
    res = tp.map(func,range(20))
    tp.shutdown()
    for i in res: # 生成器
        print(i)
    View Code

    执行结果:

    thread 0 9336
    thread 0 end
    thread 1 9336
    thread 1 end
    thread 2 9336
    thread 2 end
    thread 3 3348
    thread 3 end
    thread 4 10116
    thread 4 end
    thread 5 3348
    thread 5 endthread
    thread thread7   8 9292
    thread 8 end
    6 thread 9 9336
    thread 6 end
    thread9292
    thread 9 end
    thread 11 10 9336 9292
    
    threadthread 10 end
    thread 13 thread3348
     thread 11 end
    thread 15 thread 7 end
    thread 16 12 9292
    thread 15 end
    14 3348
    thread 16 end
    10116
    thread 14 endthread 17 3348
    thread 17 end
    thread  thread 19 3348
    thread 19 end
    
    10068933618
    
    thread 13 end
    thread 12 end 9292
    
    thread 18 end
    
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********
    ***********
    ************
    *************
    **************
    ***************
    ****************
    *****************
    ******************
    *******************
    View Code

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    Mobox企业网盘回收站文件清空与恢复的管控
    NAS设备部署后采用Mobox企业云盘来提升管理功能
    企业网盘支持对象存储帮助用户推行私有云
    阿里云登录界面无法输入账号及密码的解决方法
    团队协作管理-任务追踪管理
    windows10 家庭版 无法远程2012的解决
    bat删除多少天前的文件包含子目录
    企业网盘居然支持高速局域网文件传输工具(速度可达20M)
    防范永恒之蓝勒索病毒-XP、Win10文件共享怎样设置
    【OpenGL】学习笔记#2
  • 原文地址:https://www.cnblogs.com/lyj910313/p/10787398.html
Copyright © 2011-2022 走看看