zoukankan      html  css  js  c++  java
  • concurrent.future

    concurrent.future module provides a high-level interface for asynchronously executing callables.

    Basic Format:

    from concurrent.future import ThreadPoolExecuor,ProcessPoolExecutor
    
    executor = ThreadPoolExecutor(max_workers=2)  #max_workers 为池中允许的线程数
    
    executor = ProcessPoolExecutor(max_workers=2)
    
    obj1 = executor.submit(function, arg1, arg2)  #提交子线程
    obj2 = executor.map(func,iterator)    #异步运行的map
    
    executor.shutdown(wait=True)   # 类似于pool.close()  pool.join()  等待所有子进程/线程运行完结果  当wait = False的时候结果会立刻返回,但pool依然会等到所有结果都得到后才会释放
    
    obj1.result()
    obj2.result() #拿到结果 # ThreadPoolExecuor,ProcessPoolExecutor都可以采用with statement with ThreadPoolExecutor(max_workers=1) as executor: obj1 = executor.submit(function, arg1, arg2) #提交子线程 obj2 = executor.map(func,iterator) #异步运行的map

    Others:

    •   obj.exception(timeout=second)   

      在second内如果没有结果就报错。 如果timeout=None 就相当于obj.join()无限制等待obj结束

    •   obj.add_done_callback(func)  

      将obj返回给func函数,由于返回的obj,所以func函数内需要obj.result()

    •   obj.cancel()

      如果被执行了,就返回false,如果在执行可以被cancel 就返回true

    •   obj.cancelled

      如果被cancel成功了就返回true

    •   done()

      没有在执行就返回true(被cancel或者运行完毕)

    exmaples:

    def wait_on_future():
        f = executor.submit(pow, 5, 2)
        # This will never complete because there is only one worker thread and
        # it is executing this function.
        print(f.result())
    
    executor = ThreadPoolExecutor(max_workers=1)
    executor.submit(wait_on_future)

    reference:  http://pythonhosted.org/futures/

  • 相关阅读:
    开发人员维护测试数据库
    mybatis 自动生成 dao层 entity和xml时遇到的问题
    产品上线过程中犯下的错误
    leetcode: invert binary tree
    重构函数基本原则(持续更新)
    通过Mybatis获取mysql表中重复记录的方法
    Vmware 部分所学到的功能简写
    Vmware 主机锁定模式
    光纤卡网卡的区别以及HBA的常规定义-----引自百度百科
    存储基本概念(lun,volume,HBA,DAS,NAS,SAN,iSCSI,IPSAN)
  • 原文地址:https://www.cnblogs.com/yxi-liu/p/7677415.html
Copyright © 2011-2022 走看看