Python的并发并行[4] -> 并发[1] -> concurrent.future 模块

concurrent.future 模块


 

1 thread模块 / thread Module

 

1.1 常量 / Constants

Pass

1.2 函数 / Function

Pass

1.3 / Class

1.3.1 ThreadPoolExecutor

类实例化:executor = ThreadPoolExecutor(max_works)

类的功能:用于生成一个线程池执行器实例,其基类是_base.Executor

传入参数: max_works

max_works: int类型,线程池最大线程的数量

返回参数: executor

executor: instance类型,线程池执行器实例

1.3.1.1 submit()函数

函数调用: executor.submit(fn, *args)

函数功能:启动线程池的一个线程,调用传入的fn函数及其参数

传入参数: fn, *args

fn: function类型,传入的函数,被线程池启动的线程调用

*args: fn所需的参数,当有多个时,传入方式为submit(fn, args_1, args_2, args_3)

返回参数:

1.3.1.2 map()函数

函数调用: iter = executor.map(fn, *iterables, timeout=None)

函数功能:返回一个迭代生成器,类比于map(fn, *iterables),属于基类Executor的方法

传入参数: fn, *args, timeout

fn: function类型,传入的函数,被线程池启动的线程调用

*iterables: list/tuple等可迭代的类型,通过迭代获取,传入fn函数中。当有多个参数时,传入方式为map(fn, [a_args_1, a_args_2, ..., a_args_n], [b_args_1, b_args_2, …, b_args_n], iterable_3),map会对生成器进行迭代,分别传入参数给fn

返回参数: iter

iter: generator类型,会无序地自动调用函数进行迭代,结束后剩下None

 

2 process 模块 / process Module

TBD

 

未完待续...

 

posted @ 2018-01-01 13:58  StackLike  阅读(801)  评论(0编辑  收藏  举报