concurrent.futures --- Launching parallel tasks — Dokumentasi Python 3.15.2
Navigasi index modul | berikut | sebelum | Python » 3.15.2 documentation » The Python standard library » Eksekusi Serentak » concurrent.futures --- Launching parallel tasks | Tema | 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 a Future object 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 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 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.
With ThreadPoolExecutor and InterpreterPoolExecutor , chunksize has no effect. Berubah pada versi 3.5: Added the chunksize parameter. Berubah pada versi 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() 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.
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 with statement, which will shutdown the Executor (waiting as if Executor.shutdown() were called with wait set to True ): 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' ) Berubah pada versi 3.9: Added the cancel_futures parameter.
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: def wait_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 ) future = executor . submit ( wait_on_future ) # Note: calling future.result() would also cause a deadlock because # the single worker thread is already waiting for wait_on_future(). class concurrent.futures.
ThreadPoolExecutor ( max_workers = None , thread_name_prefix = '' , initializer = None , initargs = () , ** ctxkwargs ) ¶ An Executor subclass that uses a pool of at most max_workers threads to execute calls asynchronously. All threads enqueued to ThreadPoolExecutor will be joined before the interpreter can exit. Note that the exit handler which does this is executed before any exit handlers added using atexit . This means exceptions in the main thread must be caught and handled in order to signal threads to exit gracefully.
For this reason, it is recommended that ThreadPoolExecutor not be used for long-running tasks. initializer is an optional callable that is called at the start of each worker thread; initargs is a tuple of arguments passed to the initializer. Should initializer raise an exception, all currently pending jobs will raise a BrokenThreadPool , as well as any attempt to submit more jobs to the pool. ctxkwargs is a mapping of additional keyword arguments passed to prepare_context() , allowing customization of the worker execution context. classmethod prepare_context ( initializer , initargs ) ¶ Setting up the necessary context for creating worker instances in a pool-based executor (for example, in a concurrent environment like threads or interpreters).
Added in version 3.14. Berubah pada versi 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 . Berubah pada versi 3.6: Added the thread_name_prefix parameter to allow users to control the threading.Thread names for worker threads created by the pool for easier debugging.
Berubah pada versi 3.7: Added the initializer and initargs arguments. Berubah pada versi 3.8: Default value of max_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
The rest of this page is on the live site.