concurrent.futures — Launching parallel tasks¶
Added in version 3.2.
Source code:Lib/concurrent/futures/thread.py,Lib/concurrent/futures/process.py,andLib/concurrent/futures/interpreter.py
Theconcurrent.futures module provides a high-level interface forasynchronously executing callables.
The asynchronous execution can be performed with threads, usingThreadPoolExecutor orInterpreterPoolExecutor,or separate processes, usingProcessPoolExecutor.Each implements the same interface, which is definedby 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 as
fn(*args,**kwargs)and returns aFutureobject 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,buffersize=None)¶
Similar to
map(fn,*iterables)except:Theiterables are collected immediately rather than lazily, unless abuffersize is specified to limit the number of submitted tasks whoseresults have not yet been yielded. If the buffer is full, iteration overtheiterables pauses until a result is yielded from the buffer.
fn is executed asynchronously and several calls tofn may be made concurrently.
The returned iterator raises a
TimeoutErrorif__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 using
ProcessPoolExecutor, 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. WithThreadPoolExecutorandInterpreterPoolExecutor,chunksize has no effect.Changed in version 3.5:Added thechunksize parameter.
Changed in version 3.14:Added thebuffersize parameter.
- 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 to
Executor.submit()andExecutor.map()made after shutdown willraiseRuntimeError.Ifwait is
Truethen this method will not return until all thepending futures are done executing and the resources associated with theexecutor have been freed. Ifwait isFalsethen 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 is
True, 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 are
True, 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 the executoras acontext manager via the
withstatement, whichwill 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=())¶
An
Executorsubclass that uses a pool of at mostmax_workersthreads to execute calls asynchronously.All threads enqueued to
ThreadPoolExecutorwill 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 recommendedthatThreadPoolExecutornot 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 a
BrokenThreadPool,as well as any attempt to submit more jobs to the pool.Changed in version 3.5:Ifmax_workers is
Noneornot given, it will default to the number of processors on the machine,multiplied by5, assuming thatThreadPoolExecutoris 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 the
threading.Threadnames 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 to
min(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 to
min(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)))
InterpreterPoolExecutor¶
Added in version 3.14.
TheInterpreterPoolExecutor class uses a pool of interpretersto execute calls asynchronously. It is aThreadPoolExecutorsubclass, which means each worker is running in its own thread.The difference here is that each worker has its own interpreter,and runs each task using that interpreter.
The biggest benefit to using interpreters instead of only threadsis true multi-core parallelism. Each interpreter has its ownGlobal Interpreter Lock, so coderunning in one interpreter can run on one CPU core, while code inanother interpreter runs unblocked on a different core.
The tradeoff is that writing concurrent code for use with multipleinterpreters can take extra effort. However, this is because itforces you to be deliberate about how and when interpreters interact,and to be explicit about what data is shared between interpreters.This results in several benefits that help balance the extra effort,including true multi-core parallelism, For example, code writtenthis way can make it easier to reason about concurrency. Anothermajor benefit is that you don’t have to deal with several of thebig pain points of using threads, like race conditions.
Each worker’s interpreter is isolated from all the other interpreters.“Isolated” means each interpreter has its own runtime state andoperates completely independently. For example, if you redirectsys.stdout in one interpreter, it will not be automaticallyredirected to any other interpreter. If you import a module in oneinterpreter, it is not automatically imported in any other. Youwould need to import the module separately in interpreter whereyou need it. In fact, each module imported in an interpreter isa completely separate object from the same module in a differentinterpreter, includingsys,builtins,and even__main__.
Isolation means a mutable object, or other data, cannot be usedby more than one interpreter at the same time. That effectively meansinterpreters cannot actually share such objects or data. Instead,each interpreter must have its own copy, and you will have tosynchronize any changes between the copies manually. Immutableobjects and data, like the builtin singletons, strings, and tuplesof immutable objects, don’t have these limitations.
Communicating and synchronizing between interpreters is most effectivelydone using dedicated tools, like those proposed inPEP 734. One lessefficient alternative is to serialize withpickle and then sendthe bytes over a sharedsocket orpipe.
- classconcurrent.futures.InterpreterPoolExecutor(max_workers=None,thread_name_prefix='',initializer=None,initargs=())¶
A
ThreadPoolExecutorsubclass that executes calls asynchronouslyusing a pool of at mostmax_workers threads. Each thread runstasks in its own interpreter. The worker interpreters are isolatedfrom each other, which means each has its own runtime state and thatthey can’t share any mutable objects or other data. Each interpreterhas its ownGlobal Interpreter Lock,which means code run with this executor has true multi-core parallelism.The optionalinitializer andinitargs arguments have the samemeaning as for
ThreadPoolExecutor: the initializer is runwhen each worker is created, though in this case it is run inthe worker’s interpreter. The executor serializes theinitializerandinitargs usingpicklewhen sending them to the worker’sinterpreter.Note
The executor may replace uncaught exceptions frominitializerwith
ExecutionFailed.Other caveats from parent
ThreadPoolExecutorapply here.
submit() andmap() work like normal,except the worker serializes the callable and arguments usingpickle when sending them to its interpreter. The workerlikewise serializes the return value when sending it back.
When a worker’s current task raises an uncaught exception, the workeralways tries to preserve the exception as-is. If that is successfulthen it also sets the__cause__ to a correspondingExecutionFailedinstance, which contains a summary of the original exception.In the uncommon case that the worker is not able to preserve theoriginal as-is then it directly preserves the correspondingExecutionFailedinstance instead.
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.
Note that the restrictions on functions and arguments needing to picklable aspermultiprocessing.Process apply when usingsubmit()andmap() on aProcessPoolExecutor. A function definedin a REPL or a lambda should not be expected to work.
- classconcurrent.futures.ProcessPoolExecutor(max_workers=None,mp_context=None,initializer=None,initargs=(),max_tasks_per_child=None)¶
An
Executorsubclass that executes calls asynchronously using a poolof at mostmax_workers processes. Ifmax_workers isNoneor 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 notthenValueErrorwill be raised. Ifmax_workers isNone, thenthe default chosen will be at most61, even if more processors areavailable.mp_context can be amultiprocessingcontext orNone. It will beused to launch the workers. Ifmp_context isNoneor not given, thedefaultmultiprocessingcontext 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 a
BrokenProcessPool,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 is
Nonewhich 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, a
BrokenProcessPoolerror 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.
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 the
multiprocessingcontext 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 uses
os.process_cpu_count()by default, instead ofos.cpu_count().Changed in version 3.14:The default process start method (seeContexts and start methods) changed away fromfork. If yourequire thefork start method for
ProcessPoolExecutoryou mustexplicitly passmp_context=multiprocessing.get_context("fork").- terminate_workers()¶
Attempt to terminate all living worker processes immediately by calling
Process.terminateon each of them.Internally, it will also callExecutor.shutdown()to ensure that allother resources associated with the executor are freed.After calling this method the caller should no longer submit tasks to theexecutor.
Added in version 3.14.
- kill_workers()¶
Attempt to kill all living worker processes immediately by calling
Process.killon each of them.Internally, it will also callExecutor.shutdown()to ensure that allother resources associated with the executor are freed.After calling this method the caller should no longer submit tasks to theexecutor.
Added in version 3.14.
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 return
False, otherwise the call will be cancelled and the method willreturnTrue.
- cancelled()¶
Return
Trueif the call was successfully cancelled.
- running()¶
Return
Trueif the call is currently being executed and cannot becancelled.
- done()¶
Return
Trueif 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 a
TimeoutErrorwill 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 then
CancelledErrorwill 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 a
TimeoutErrorwill 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 then
CancelledErrorwill be raised.If the call completed without raising,
Noneis 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 an
Exceptionsubclass, it will be logged andignored. If the callable raises aBaseExceptionsubclass, thebehavior is undefined.If the future has already completed or been cancelled,fn will becalled immediately.
The following
Futuremethods are meant for use in unit tests andExecutorimplementations.- set_running_or_notify_cancel()¶
This method should only be called by
Executorimplementationsbefore executing the work associated with theFutureand by unittests.If the method returns
Falsethen theFuturewas cancelled,i.e.Future.cancel()was called and returnedTrue. Any threadswaiting on theFuturecompleting (i.e. throughas_completed()orwait()) will be woken up.If the method returns
Truethen theFuturewas 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 after
Future.set_result()orFuture.set_exception()have beencalled.
- set_result(result)¶
Sets the result of the work associated with the
Futuretoresult.This method should only be used by
Executorimplementations andunit tests.Changed in version 3.8:This method raises
concurrent.futures.InvalidStateErrorif theFutureisalready done.
Module Functions¶
- concurrent.futures.wait(fs,timeout=None,return_when=ALL_COMPLETED)¶
Wait for the
Futureinstances (possibly created by differentExecutorinstances) 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 specifiedor
None, 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 to
ALL_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 the
Futureinstances (possibly created bydifferentExecutorinstances) 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 aTimeoutErrorif__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 of
TimeoutError,raised when a future operation exceeds the given timeout.Changed in version 3.11:This class was made an alias of
TimeoutError.
- exceptionconcurrent.futures.BrokenExecutor¶
Derived from
RuntimeError, 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 from
BrokenExecutor, this exceptionclass is raised when one of the workersof aThreadPoolExecutorhas failed initializing.Added in version 3.7.
- exceptionconcurrent.futures.interpreter.BrokenInterpreterPool¶
Derived from
BrokenThreadPool,this exception class is raised when one of the workersof aInterpreterPoolExecutorhas failed initializing.Added in version 3.14.
- exceptionconcurrent.futures.interpreter.ExecutionFailed¶
Raised from
InterpreterPoolExecutorwhenthe given initializer fails or fromsubmit()when there’s an uncaughtexception from the submitted task.Added in version 3.14.
- exceptionconcurrent.futures.process.BrokenProcessPool¶
Derived from
BrokenExecutor(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.