concurrent.futures — 发起并行任务

Added in version 3.2.

源代码: Lib/concurrent/futures/thread.py and Lib/concurrent/futures/process.py


The concurrent.futures 模块为异步执行可调用提供高级接口。

异步执行可以采用线程履行使用 ThreadPoolExecutor ,或单独进程使用 ProcessPoolExecutor 。两者实现相同接口,定义通过抽象 Executor 类。

可用性 :非 WASI。

This module does not work or is not available on WebAssembly. See WebAssembly 平台 了解更多信息。

执行器对象

class concurrent.futures. Executor

提供异步执行调用的方法的抽象类。不应直接使用它,但应透过它的具体子类。

submit ( fn , / , * args , ** kwargs )

调度可调用 fn 以执行按 fn(*args, **kwargs) 并返回 Future 对象表示可调用的执行。

with ThreadPoolExecutor(max_workers=1) as executor:
    future = executor.submit(pow, 323, 1235)
    print(future.result())