Equivalent to map(func,*iterables) except func is executed
asynchronously and several calls to func may be made concurrently. The
returned iterator raises a concurrent.futures.TimeoutError if
__next__() is called and the result isn’t available
after timeout seconds from the original call to Executor.map().
timeout can be an int or a float. If timeout is not specified or
None, there is no limit to the wait time. If a 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. With ThreadPoolExecutor,
chunksize has no effect.

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() and Executor.map() made after shutdown will
raise RuntimeError.

If wait is True then 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 is False then 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.

You can avoid having to call this method explicitly if you use the
with statement, which will shutdown the Executor
(waiting as if Executor.shutdown() were called with wait set to
True):

Deadlocks can occur when the callable associated with a Future waits on
the results of another Future. For example:

importtimedefwait_on_b():time.sleep(5)print(b.result())# b will never complete because it is waiting on a.return5defwait_on_a():time.sleep(5)print(a.result())# a will never complete because it is waiting on b.return6executor=ThreadPoolExecutor(max_workers=2)a=executor.submit(wait_on_b)b=executor.submit(wait_on_a)

And:

defwait_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)

An Executor subclass that uses a pool of at most max_workers
threads to execute calls asynchronously.

Changed in version 3.5: If max_workers is None or
not given, it will default to the number of processors on the machine,
multiplied by 5, assuming that ThreadPoolExecutor is often
used to overlap I/O instead of CPU work and the number of workers
should be higher than the number of workers
for ProcessPoolExecutor.

New in version 3.6: The thread_name_prefix argument was added to allow users to
control the threading.Thread names for worker threads created by
the pool for easier debugging.

importconcurrent.futuresimporturllib.requestURLS=['http://www.foxnews.com/','http://www.cnn.com/','http://europe.wsj.com/','http://www.bbc.co.uk/','http://some-made-up-domain.com/']# Retrieve a single page and report the URL and contentsdefload_url(url,timeout):withurllib.request.urlopen(url,timeout=timeout)asconn:returnconn.read()# We can use a with statement to ensure threads are cleaned up promptlywithconcurrent.futures.ThreadPoolExecutor(max_workers=5)asexecutor:# Start the load operations and mark each future with its URLfuture_to_url={executor.submit(load_url,url,60):urlforurlinURLS}forfutureinconcurrent.futures.as_completed(future_to_url):url=future_to_url[future]try:data=future.result()exceptExceptionasexc:print('%r generated an exception: %s'%(url,exc))else:print('%r page is %d bytes'%(url,len(data)))

An Executor subclass that executes calls asynchronously using a pool
of at most max_workers processes. If max_workers is None or not
given, it will default to the number of processors on the machine.
If max_workers is lower or equal to 0, then a ValueError
will be raised.

Changed in version 3.3: When one of the worker processes terminates abruptly, a
BrokenProcessPool error is now raised. Previously, behaviour
was undefined but operations on the executor or its futures would often
freeze or deadlock.

Return the value returned by the call. If the call hasn’t yet completed
then this method will wait up to timeout seconds. If the call hasn’t
completed in timeout seconds, then a
concurrent.futures.TimeoutError will be raised. timeout can be
an int or float. If timeout is not specified or None, there is no
limit to the wait time.

If the future is cancelled before completing then CancelledError
will be raised.

Return the exception raised by the call. If the call hasn’t yet
completed then this method will wait up to timeout seconds. If the
call hasn’t completed in timeout seconds, then a
concurrent.futures.TimeoutError will be raised. timeout can be
an int or float. If timeout is not specified or None, there is no
limit to the wait time.

If the future is cancelled before completing then CancelledError
will be raised.

Attaches the callable fn to the future. fn will be called, with the
future as its only argument, when the future is cancelled or finishes
running.

Added callables are called in the order that they were added and are
always called in a thread belonging to the process that added them. If
the callable raises an Exception subclass, it will be logged and
ignored. If the callable raises a BaseException subclass, the
behavior is undefined.

If the future has already completed or been cancelled, fn will be
called immediately.

The following Future methods are meant for use in unit tests and
Executor implementations.

Wait for the Future instances (possibly created by different
Executor instances) given by fs to complete. Returns a named
2-tuple of sets. The first set, named done, contains the futures that
completed (finished or were cancelled) before the wait completed. The second
set, named not_done, contains uncompleted futures.

timeout can be used to control the maximum number of seconds to wait before
returning. timeout can be an int or float. If timeout is not specified
or None, there is no limit to the wait time.

return_when indicates when this function should return. It must be one of
the following constants:

Constant

Description

FIRST_COMPLETED

The function will return when any
future finishes or is cancelled.

FIRST_EXCEPTION

The function will return when any
future finishes by raising an
exception. If no future raises an
exception then it is equivalent to
ALL_COMPLETED.

Returns an iterator over the Future instances (possibly created by
different Executor instances) given by fs that yields futures as
they complete (finished or were cancelled). Any futures given by fs that
are duplicated will be returned once. Any futures that completed before
as_completed() is called will be yielded first. The returned iterator
raises a concurrent.futures.TimeoutError if __next__()
is called and the result isn’t available after timeout seconds from the
original call to as_completed(). timeout can be an int or float. If
timeout is not specified or None, there is no limit to the wait time.