concurrent.futures — Launching parallel tasks

Added in version 3.2.

Source code:Lib/concurrent/futures/thread.pyandLib/concurrent/futures/process.py


Theconcurrent.futures module provides a high-level interface forasynchronously executing callables.

The asynchronous execution can be performed with threads, usingThreadPoolExecutor, or separate processes, usingProcessPoolExecutor. Both implement the same interface, which isdefined by the abstractExecutor class.

Availability: not WASI.

This module does not work or is not available on WebAssembly. SeeWebAssembly platforms for more information.

Executor Objects

classconcurrent.futures.Executor

An abstract class that provides methods to execute calls asynchronously. Itshould not be used directly, but through its concrete subclasses.

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

Schedules the callable,fn, to be executed asfn(*args,**kwargs)and returns aFuture object representing the execution of thecallable.

withThreadPoolExecutor(max_workers=1)asexecutor:future=executor.submit(pow,323,1235)print(future.result())
map(fn,*iterables,timeout=None,chunksize=1)

Similar tomap(fn,*iterables) except:

  • theiterables are collected immediately rather than lazily;

  • fn is executed asynchronously and several calls tofn may be made concurrently.

The returned iterator raises aTimeoutErrorif__next__() is called and the result isn’t availableaftertimeout seconds from the original call toExecutor.map().timeout can be an int or a float. Iftimeout is not specified orNone, there is no limit to the wait time.

If afn call raises an exception, then that exception will beraised when its value is retrieved from the iterator.

When usingProcessPoolExecutor, this method chopsiterablesinto a number of chunks which it submits to the pool as separatetasks. The (approximate) size of these chunks can be specified bysettingchunksize to a positive integer. For very long iterables,using a large value forchunksize can significantly improveperformance compared to the default size of 1. WithThreadPoolExecutor,chunksize has no effect.

Changed in version 3.5:Added thechunksize argument.

shutdown(wait=True,*,cancel_futures=False)

Signal the executor that it should free any resources that it is usingwhen the currently pending futures are done executing. Calls toExecutor.submit() andExecutor.map() made after shutdown willraiseRuntimeError.

Ifwait isTrue then this method will not return until all thepending futures are done executing and the resources associated with theexecutor have been freed. Ifwait isFalse then this method willreturn immediately and the resources associated with the executor will befreed when all pending futures are done executing. Regardless of thevalue ofwait, the entire Python program will not exit until allpending futures are done executing.

Ifcancel_futures isTrue, this method will cancel all pendingfutures that the executor has not started running. Any futures thatare completed or running won’t be cancelled, regardless of the valueofcancel_futures.

If bothcancel_futures andwait areTrue, all futures that theexecutor has started running will be completed prior to this methodreturning. The remaining futures are cancelled.

You can avoid having to call this method explicitly if you use thewith statement, which will shutdown theExecutor(waiting as ifExecutor.shutdown() were called withwait set toTrue):

importshutilwithThreadPoolExecutor(max_workers=4)ase: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:Addedcancel_futures.

ThreadPoolExecutor

ThreadPoolExecutor is anExecutor subclass that uses a pool ofthreads to execute calls asynchronously.

Deadlocks can occur when the callable associated with aFuture waits onthe results of anotherFuture. 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)
classconcurrent.futures.ThreadPoolExecutor(max_workers=None,thread_name_prefix='',initializer=None,initargs=())

AnExecutor subclass that uses a pool of at mostmax_workersthreads to execute calls asynchronously.

All threads enqueued toThreadPoolExecutor will be joined before theinterpreter can exit. Note that the exit handler which does this isexecutedbefore any exit handlers added usingatexit. This meansexceptions in the main thread must be caught and handled in order tosignal threads to exit gracefully. For this reason, it is recommendedthatThreadPoolExecutor not be used for long-running tasks.

initializer is an optional callable that is called at the start ofeach worker thread;initargs is a tuple of arguments passed to theinitializer. Shouldinitializer raise an exception, all currentlypending jobs will raise aBrokenThreadPool,as well as any attempt to submit more jobs to the pool.

Changed in version 3.5:Ifmax_workers isNone ornot given, it will default to the number of processors on the machine,multiplied by5, assuming thatThreadPoolExecutor is oftenused to overlap I/O instead of CPU work and the number of workersshould be higher than the number of workersforProcessPoolExecutor.

Changed in version 3.6:Added thethread_name_prefix parameter to allow users tocontrol thethreading.Thread names for worker threads created bythe pool for easier debugging.

Changed in version 3.7:Added theinitializer andinitargs arguments.

Changed in version 3.8:Default value ofmax_workers is changed tomin(32,os.cpu_count()+4).This default value preserves at least 5 workers for I/O bound tasks.It utilizes at most 32 CPU cores for CPU bound tasks which release the GIL.And it avoids using very large resources implicitly on many-core machines.

ThreadPoolExecutor now reuses idle worker threads before startingmax_workers worker threads too.

Changed in version 3.13:Default value ofmax_workers is changed tomin(32,(os.process_cpu_count()or1)+4).

ThreadPoolExecutor Example

importconcurrent.futuresimporturllib.requestURLS=['http://www.foxnews.com/','http://www.cnn.com/','http://europe.wsj.com/','http://www.bbc.co.uk/','http://nonexistent-subdomain.python.org/']# 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)))

ProcessPoolExecutor

TheProcessPoolExecutor class is anExecutor subclass thatuses a pool of processes to execute calls asynchronously.ProcessPoolExecutor uses themultiprocessing module, whichallows it to side-step theGlobal Interpreter Lock but also means thatonly picklable objects can be executed and returned.

The__main__ module must be importable by worker subprocesses. This meansthatProcessPoolExecutor will not work in the interactive interpreter.

CallingExecutor orFuture methods from a callable submittedto aProcessPoolExecutor will result in deadlock.

classconcurrent.futures.ProcessPoolExecutor(max_workers=None,mp_context=None,initializer=None,initargs=(),max_tasks_per_child=None)

AnExecutor subclass that executes calls asynchronously using a poolof at mostmax_workers processes. Ifmax_workers isNone or notgiven, it will default toos.process_cpu_count().Ifmax_workers is less than or equal to0, then aValueErrorwill be raised.On Windows,max_workers must be less than or equal to61. If it is notthenValueError will be raised. Ifmax_workers isNone, thenthe default chosen will be at most61, even if more processors areavailable.mp_context can be amultiprocessing context orNone. It will beused to launch the workers. Ifmp_context isNone or not given, thedefaultmultiprocessing context is used.SeeContexts and start methods.

initializer is an optional callable that is called at the start ofeach worker process;initargs is a tuple of arguments passed to theinitializer. Shouldinitializer raise an exception, all currentlypending jobs will raise aBrokenProcessPool,as well as any attempt to submit more jobs to the pool.

max_tasks_per_child is an optional argument that specifies the maximumnumber of tasks a single process can execute before it will exit and bereplaced with a fresh worker process. By defaultmax_tasks_per_child isNone which means worker processes will live as long as the pool. Whena max is specified, the “spawn” multiprocessing start method will be used bydefault in absence of amp_context parameter. This feature is incompatiblewith the “fork” start method.

Changed in version 3.3:When one of the worker processes terminates abruptly, aBrokenProcessPool error is now raised.Previously, behaviourwas undefined but operations on the executor or its futures would oftenfreeze or deadlock.

Changed in version 3.7:Themp_context argument was added to allow users to control thestart_method for worker processes created by the pool.

Added theinitializer andinitargs arguments.

Note

The defaultmultiprocessing start method(seeContexts and start methods) will change away fromfork in Python 3.14. Code that requiresfork be used for theirProcessPoolExecutor should explicitly specify that bypassing amp_context=multiprocessing.get_context("fork")parameter.

Changed in version 3.11:Themax_tasks_per_child argument was added to allow users tocontrol the lifetime of workers in the pool.

Changed in version 3.12:On POSIX systems, if your application has multiple threads and themultiprocessing context uses the"fork" start method:Theos.fork() function called internally to spawn workers may raise aDeprecationWarning. Pass amp_context configured to use adifferent start method. See theos.fork() documentation forfurther explanation.

Changed in version 3.13:max_workers usesos.process_cpu_count() by default, instead ofos.cpu_count().

ProcessPoolExecutor Example

importconcurrent.futuresimportmathPRIMES=[112272535095293,112582705942171,112272535095293,115280095190773,115797848077099,1099726899285419]defis_prime(n):ifn<2:returnFalseifn==2:returnTrueifn%2==0:returnFalsesqrt_n=int(math.floor(math.sqrt(n)))foriinrange(3,sqrt_n+1,2):ifn%i==0:returnFalsereturnTruedefmain():withconcurrent.futures.ProcessPoolExecutor()asexecutor:fornumber,primeinzip(PRIMES,executor.map(is_prime,PRIMES)):print('%d is prime:%s'%(number,prime))if__name__=='__main__':main()

Future Objects

TheFuture class encapsulates the asynchronous execution of a callable.Future instances are created byExecutor.submit().

classconcurrent.futures.Future

Encapsulates the asynchronous execution of a callable.Futureinstances are created byExecutor.submit() and should not be createddirectly except for testing.

cancel()

Attempt to cancel the call. If the call is currently being executed orfinished running and cannot be cancelled then the method will returnFalse, otherwise the call will be cancelled and the method willreturnTrue.

cancelled()

ReturnTrue if the call was successfully cancelled.

running()

ReturnTrue if the call is currently being executed and cannot becancelled.

done()

ReturnTrue if the call was successfully cancelled or finishedrunning.

result(timeout=None)

Return the value returned by the call. If the call hasn’t yet completedthen this method will wait up totimeout seconds. If the call hasn’tcompleted intimeout seconds, then aTimeoutError will be raised.timeout can bean int or float. Iftimeout is not specified orNone, there is nolimit to the wait time.

If the future is cancelled before completing thenCancelledErrorwill be raised.

If the call raised an exception, this method will raise the same exception.

exception(timeout=None)

Return the exception raised by the call. If the call hasn’t yetcompleted then this method will wait up totimeout seconds. If thecall hasn’t completed intimeout seconds, then aTimeoutError will be raised.timeout can bean int or float. Iftimeout is not specified orNone, there is nolimit to the wait time.

If the future is cancelled before completing thenCancelledErrorwill be raised.

If the call completed without raising,None is returned.

add_done_callback(fn)

Attaches the callablefn to the future.fn will be called, with thefuture as its only argument, when the future is cancelled or finishesrunning.

Added callables are called in the order that they were added and arealways called in a thread belonging to the process that added them. Ifthe callable raises anException subclass, it will be logged andignored. If the callable raises aBaseException subclass, thebehavior is undefined.

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

The followingFuture methods are meant for use in unit tests andExecutor implementations.

set_running_or_notify_cancel()

This method should only be called byExecutor implementationsbefore executing the work associated with theFuture and by unittests.

If the method returnsFalse then theFuture was cancelled,i.e.Future.cancel() was called and returnedTrue. Any threadswaiting on theFuture completing (i.e. throughas_completed() orwait()) will be woken up.

If the method returnsTrue then theFuture was not cancelledand has been put in the running state, i.e. calls toFuture.running() will returnTrue.

This method can only be called once and cannot be called afterFuture.set_result() orFuture.set_exception() have beencalled.

set_result(result)

Sets the result of the work associated with theFuture toresult.

This method should only be used byExecutor implementations andunit tests.

Changed in version 3.8:This method raisesconcurrent.futures.InvalidStateError if theFuture isalready done.

set_exception(exception)

Sets the result of the work associated with theFuture to theExceptionexception.

This method should only be used byExecutor implementations andunit tests.

Changed in version 3.8:This method raisesconcurrent.futures.InvalidStateError if theFuture isalready done.

Module Functions

concurrent.futures.wait(fs,timeout=None,return_when=ALL_COMPLETED)

Wait for theFuture instances (possibly created by differentExecutor instances) given byfs to complete. Duplicate futuresgiven tofs are removed and will be returned only once. Returns a named2-tuple of sets. The first set, nameddone, contains the futures thatcompleted (finished or cancelled futures) before the wait completed. Thesecond set, namednot_done, contains the futures that did not complete(pending or running futures).

timeout can be used to control the maximum number of seconds to wait beforereturning.timeout can be an int or float. Iftimeout is not specifiedorNone, there is no limit to the wait time.

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

Constant

Description

concurrent.futures.FIRST_COMPLETED

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

concurrent.futures.FIRST_EXCEPTION

The function will return when any future finishes by raising anexception. If no future raises an exceptionthen it is equivalent toALL_COMPLETED.

concurrent.futures.ALL_COMPLETED

The function will return when all futures finish or are cancelled.

concurrent.futures.as_completed(fs,timeout=None)

Returns an iterator over theFuture instances (possibly created bydifferentExecutor instances) given byfs that yields futures asthey complete (finished or cancelled futures). Any futures given byfs thatare duplicated will be returned once. Any futures that completed beforeas_completed() is called will be yielded first. The returned iteratorraises aTimeoutError if__next__()is called and the result isn’t available aftertimeout seconds from theoriginal call toas_completed().timeout can be an int or float. Iftimeout is not specified orNone, there is no limit to the wait time.

See also

PEP 3148 – futures - execute computations asynchronously

The proposal which described this feature for inclusion in the Pythonstandard library.

Exception classes

exceptionconcurrent.futures.CancelledError

Raised when a future is cancelled.

exceptionconcurrent.futures.TimeoutError

A deprecated alias ofTimeoutError,raised when a future operation exceeds the given timeout.

Changed in version 3.11:This class was made an alias ofTimeoutError.

exceptionconcurrent.futures.BrokenExecutor

Derived fromRuntimeError, this exception class is raisedwhen an executor is broken for some reason, and cannot be usedto submit or execute new tasks.

Added in version 3.7.

exceptionconcurrent.futures.InvalidStateError

Raised when an operation is performed on a future that is not allowedin the current state.

Added in version 3.8.

exceptionconcurrent.futures.thread.BrokenThreadPool

Derived fromBrokenExecutor, this exceptionclass is raised when one of the workersof aThreadPoolExecutorhas failed initializing.

Added in version 3.7.

exceptionconcurrent.futures.process.BrokenProcessPool

Derived fromBrokenExecutor (formerlyRuntimeError), this exception class is raised when one of theworkers of aProcessPoolExecutorhas terminated in a non-cleanfashion (for example, if it was killed from the outside).

Added in version 3.3.