concurrent.futures — Launching parallel tasks¶
Added in version 3.2.
Source code: Lib/concurrent/futures/thread.py, Lib/concurrent/futures/process.py, and Lib/concurrent/futures/interpreter.py
The concurrent.futures module provides a high-level interface for
asynchronously executing callables.
The asynchronous execution can be performed with threads, using
ThreadPoolExecutor or InterpreterPoolExecutor,
or separate processes, using ProcessPoolExecutor.
Each implements the same interface, which is defined
by the abstract Executor class.
concurrent.futures.Future must not be confused with
asyncio.Future, which is designed for use with asyncio
tasks and coroutines. See the asyncio’s Future
documentation for a detailed comparison of the two.
Availability: not WASI.
This module does not work or is not available on WebAssembly. See WebAssembly platforms for more information.
Executor Objects¶
- class concurrent.futures.Executor¶
An abstract class that provides methods to execute calls asynchronously. It should not be used directly, but through its concrete subclasses.
- submit(fn, /, *args, **kwargs)¶
Schedules the callable, fn, to be executed as
fn(*args, **kwargs)and returns aFutureobject representing the execution of the callable.with ThreadPoolExecutor(max_workers=1) as executor: future = executor.submit(pow, 323, 1235) print(future.result())
- map(fn, *iterables, timeout=None, chunksize=1, buffersize=None)¶
Similar to
map(fn, *iterables)except:The iterables are collected immediately rather than lazily, unless a buffersize is specified to limit the number of submitted tasks whose results have not yet been yielded. If the buffer is full, iteration over the iterables pauses until a result is yielded from the buffer.
fn is executed asynchronously and several calls to fn may be made concurrently.
The returned iterator raises a
TimeoutErrorif__next__()is called and the result isn’t available after timeout seconds from the original call toExecutor.map(). timeout can be an int or a float. If timeout is not specified orNone, there is no limit to the wait time.If a fn call raises an exception, then that exception will be raised when its value is retrieved from the iterator.
When using
ProcessPoolExecutor, this method chops iterables into a number of chunks which it submits to the pool as separate tasks. The (approximate) size of these chunks can be specified by setting chunksize to a positive integer. For very long iterables, using a large value for chunksize can significantly improve performance compared to the default size of 1. WithThreadPoolExecutorandInterpreterPoolExecutor, chunksize has no effect.Changed in version 3.5: Added the chunksize parameter.
Changed in version 3.14: Added the buffersize parameter.
- shutdown(wait=True, *, cancel_futures=False)¶
Signal the executor that it should free any resources that it is using when the currently pending futures are done executing. Calls to
Executor.submit()andExecutor.map()made after shutdown will raiseRuntimeError.If wait is
Truethen this method will not return until all the pending futures are done executing and the resources associated with the executor have been freed. If wait isFalsethen this method will return immediately and the resources associated with the executor will be freed when all pending futures are done executing. Regardless of the value of wait, the entire Python program will not exit until all pending futures are done executing.If cancel_futures is
True, this method will cancel all pending futures that the executor has not started running. Any futures that are completed or running won’t be cancelled, regardless of the value of cancel_futures.If both cancel_futures and wait are
True, all futures that the executor has started running will be completed prior to this method returning. The remaining futures are cancelled.You can avoid having to call this method explicitly if you use the executor as a context manager via the
withstatement, which will shutdown theExecutor(waiting as ifExecutor.shutdown()were called with wait set toTrue):import shutil with ThreadPoolExecutor(max_workers=4) as e: e.submit(shutil.copy, 'src1.txt', 'dest1.txt') e.submit(shutil.copy, 'src2.txt', 'dest2.txt') e.submit(shutil.copy, 'src3.txt', 'dest3.txt') e.submit(shutil.copy, 'src4.txt', 'dest4.txt')
Changed in version 3.9: Added cancel_futures.
ThreadPoolExecutor¶
ThreadPoolExecutor is an Executor subclass that uses a pool of
threads to execute calls asynchronously.
Deadlocks can occur when the callable associated with a Future waits on
the results of another Future. For example:
import time
def wait_on_b():
time.sleep(5)
print(b.result()) # b will never complete because it is waiting on a.
return 5
def wait_on_a():
time.sleep(5)
print(a.result()) # a will never complete because it is waiting on b.
return 6
executor = ThreadPoolExecutor(max_workers=2)
a = executor.submit(wait_on_b)
b = executor.submit(wait_on_a)
And: