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/

原文地址:https://www.cnblogs.com/yxi-liu/p/7677415.html