17db96d56Sopenharmony_ci:mod:`concurrent.futures` --- Launching parallel tasks 27db96d56Sopenharmony_ci====================================================== 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ci.. module:: concurrent.futures 57db96d56Sopenharmony_ci :synopsis: Execute computations concurrently using threads or processes. 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ci.. versionadded:: 3.2 87db96d56Sopenharmony_ci 97db96d56Sopenharmony_ci**Source code:** :source:`Lib/concurrent/futures/thread.py` 107db96d56Sopenharmony_ciand :source:`Lib/concurrent/futures/process.py` 117db96d56Sopenharmony_ci 127db96d56Sopenharmony_ci-------------- 137db96d56Sopenharmony_ci 147db96d56Sopenharmony_ciThe :mod:`concurrent.futures` module provides a high-level interface for 157db96d56Sopenharmony_ciasynchronously executing callables. 167db96d56Sopenharmony_ci 177db96d56Sopenharmony_ciThe asynchronous execution can be performed with threads, using 187db96d56Sopenharmony_ci:class:`ThreadPoolExecutor`, or separate processes, using 197db96d56Sopenharmony_ci:class:`ProcessPoolExecutor`. Both implement the same interface, which is 207db96d56Sopenharmony_cidefined by the abstract :class:`Executor` class. 217db96d56Sopenharmony_ci 227db96d56Sopenharmony_ci.. include:: ../includes/wasm-notavail.rst 237db96d56Sopenharmony_ci 247db96d56Sopenharmony_ciExecutor Objects 257db96d56Sopenharmony_ci---------------- 267db96d56Sopenharmony_ci 277db96d56Sopenharmony_ci.. class:: Executor 287db96d56Sopenharmony_ci 297db96d56Sopenharmony_ci An abstract class that provides methods to execute calls asynchronously. It 307db96d56Sopenharmony_ci should not be used directly, but through its concrete subclasses. 317db96d56Sopenharmony_ci 327db96d56Sopenharmony_ci .. method:: submit(fn, /, *args, **kwargs) 337db96d56Sopenharmony_ci 347db96d56Sopenharmony_ci Schedules the callable, *fn*, to be executed as ``fn(*args, **kwargs)`` 357db96d56Sopenharmony_ci and returns a :class:`Future` object representing the execution of the 367db96d56Sopenharmony_ci callable. :: 377db96d56Sopenharmony_ci 387db96d56Sopenharmony_ci with ThreadPoolExecutor(max_workers=1) as executor: 397db96d56Sopenharmony_ci future = executor.submit(pow, 323, 1235) 407db96d56Sopenharmony_ci print(future.result()) 417db96d56Sopenharmony_ci 427db96d56Sopenharmony_ci .. method:: map(func, *iterables, timeout=None, chunksize=1) 437db96d56Sopenharmony_ci 447db96d56Sopenharmony_ci Similar to :func:`map(func, *iterables) <map>` except: 457db96d56Sopenharmony_ci 467db96d56Sopenharmony_ci * the *iterables* are collected immediately rather than lazily; 477db96d56Sopenharmony_ci 487db96d56Sopenharmony_ci * *func* is executed asynchronously and several calls to 497db96d56Sopenharmony_ci *func* may be made concurrently. 507db96d56Sopenharmony_ci 517db96d56Sopenharmony_ci The returned iterator raises a :exc:`TimeoutError` 527db96d56Sopenharmony_ci if :meth:`~iterator.__next__` is called and the result isn't available 537db96d56Sopenharmony_ci after *timeout* seconds from the original call to :meth:`Executor.map`. 547db96d56Sopenharmony_ci *timeout* can be an int or a float. If *timeout* is not specified or 557db96d56Sopenharmony_ci ``None``, there is no limit to the wait time. 567db96d56Sopenharmony_ci 577db96d56Sopenharmony_ci If a *func* call raises an exception, then that exception will be 587db96d56Sopenharmony_ci raised when its value is retrieved from the iterator. 597db96d56Sopenharmony_ci 607db96d56Sopenharmony_ci When using :class:`ProcessPoolExecutor`, this method chops *iterables* 617db96d56Sopenharmony_ci into a number of chunks which it submits to the pool as separate 627db96d56Sopenharmony_ci tasks. The (approximate) size of these chunks can be specified by 637db96d56Sopenharmony_ci setting *chunksize* to a positive integer. For very long iterables, 647db96d56Sopenharmony_ci using a large value for *chunksize* can significantly improve 657db96d56Sopenharmony_ci performance compared to the default size of 1. With 667db96d56Sopenharmony_ci :class:`ThreadPoolExecutor`, *chunksize* has no effect. 677db96d56Sopenharmony_ci 687db96d56Sopenharmony_ci .. versionchanged:: 3.5 697db96d56Sopenharmony_ci Added the *chunksize* argument. 707db96d56Sopenharmony_ci 717db96d56Sopenharmony_ci .. method:: shutdown(wait=True, *, cancel_futures=False) 727db96d56Sopenharmony_ci 737db96d56Sopenharmony_ci Signal the executor that it should free any resources that it is using 747db96d56Sopenharmony_ci when the currently pending futures are done executing. Calls to 757db96d56Sopenharmony_ci :meth:`Executor.submit` and :meth:`Executor.map` made after shutdown will 767db96d56Sopenharmony_ci raise :exc:`RuntimeError`. 777db96d56Sopenharmony_ci 787db96d56Sopenharmony_ci If *wait* is ``True`` then this method will not return until all the 797db96d56Sopenharmony_ci pending futures are done executing and the resources associated with the 807db96d56Sopenharmony_ci executor have been freed. If *wait* is ``False`` then this method will 817db96d56Sopenharmony_ci return immediately and the resources associated with the executor will be 827db96d56Sopenharmony_ci freed when all pending futures are done executing. Regardless of the 837db96d56Sopenharmony_ci value of *wait*, the entire Python program will not exit until all 847db96d56Sopenharmony_ci pending futures are done executing. 857db96d56Sopenharmony_ci 867db96d56Sopenharmony_ci If *cancel_futures* is ``True``, this method will cancel all pending 877db96d56Sopenharmony_ci futures that the executor has not started running. Any futures that 887db96d56Sopenharmony_ci are completed or running won't be cancelled, regardless of the value 897db96d56Sopenharmony_ci of *cancel_futures*. 907db96d56Sopenharmony_ci 917db96d56Sopenharmony_ci If both *cancel_futures* and *wait* are ``True``, all futures that the 927db96d56Sopenharmony_ci executor has started running will be completed prior to this method 937db96d56Sopenharmony_ci returning. The remaining futures are cancelled. 947db96d56Sopenharmony_ci 957db96d56Sopenharmony_ci You can avoid having to call this method explicitly if you use the 967db96d56Sopenharmony_ci :keyword:`with` statement, which will shutdown the :class:`Executor` 977db96d56Sopenharmony_ci (waiting as if :meth:`Executor.shutdown` were called with *wait* set to 987db96d56Sopenharmony_ci ``True``):: 997db96d56Sopenharmony_ci 1007db96d56Sopenharmony_ci import shutil 1017db96d56Sopenharmony_ci with ThreadPoolExecutor(max_workers=4) as e: 1027db96d56Sopenharmony_ci e.submit(shutil.copy, 'src1.txt', 'dest1.txt') 1037db96d56Sopenharmony_ci e.submit(shutil.copy, 'src2.txt', 'dest2.txt') 1047db96d56Sopenharmony_ci e.submit(shutil.copy, 'src3.txt', 'dest3.txt') 1057db96d56Sopenharmony_ci e.submit(shutil.copy, 'src4.txt', 'dest4.txt') 1067db96d56Sopenharmony_ci 1077db96d56Sopenharmony_ci .. versionchanged:: 3.9 1087db96d56Sopenharmony_ci Added *cancel_futures*. 1097db96d56Sopenharmony_ci 1107db96d56Sopenharmony_ci 1117db96d56Sopenharmony_ciThreadPoolExecutor 1127db96d56Sopenharmony_ci------------------ 1137db96d56Sopenharmony_ci 1147db96d56Sopenharmony_ci:class:`ThreadPoolExecutor` is an :class:`Executor` subclass that uses a pool of 1157db96d56Sopenharmony_cithreads to execute calls asynchronously. 1167db96d56Sopenharmony_ci 1177db96d56Sopenharmony_ciDeadlocks can occur when the callable associated with a :class:`Future` waits on 1187db96d56Sopenharmony_cithe results of another :class:`Future`. For example:: 1197db96d56Sopenharmony_ci 1207db96d56Sopenharmony_ci import time 1217db96d56Sopenharmony_ci def wait_on_b(): 1227db96d56Sopenharmony_ci time.sleep(5) 1237db96d56Sopenharmony_ci print(b.result()) # b will never complete because it is waiting on a. 1247db96d56Sopenharmony_ci return 5 1257db96d56Sopenharmony_ci 1267db96d56Sopenharmony_ci def wait_on_a(): 1277db96d56Sopenharmony_ci time.sleep(5) 1287db96d56Sopenharmony_ci print(a.result()) # a will never complete because it is waiting on b. 1297db96d56Sopenharmony_ci return 6 1307db96d56Sopenharmony_ci 1317db96d56Sopenharmony_ci 1327db96d56Sopenharmony_ci executor = ThreadPoolExecutor(max_workers=2) 1337db96d56Sopenharmony_ci a = executor.submit(wait_on_b) 1347db96d56Sopenharmony_ci b = executor.submit(wait_on_a) 1357db96d56Sopenharmony_ci 1367db96d56Sopenharmony_ciAnd:: 1377db96d56Sopenharmony_ci 1387db96d56Sopenharmony_ci def wait_on_future(): 1397db96d56Sopenharmony_ci f = executor.submit(pow, 5, 2) 1407db96d56Sopenharmony_ci # This will never complete because there is only one worker thread and 1417db96d56Sopenharmony_ci # it is executing this function. 1427db96d56Sopenharmony_ci print(f.result()) 1437db96d56Sopenharmony_ci 1447db96d56Sopenharmony_ci executor = ThreadPoolExecutor(max_workers=1) 1457db96d56Sopenharmony_ci executor.submit(wait_on_future) 1467db96d56Sopenharmony_ci 1477db96d56Sopenharmony_ci 1487db96d56Sopenharmony_ci.. class:: ThreadPoolExecutor(max_workers=None, thread_name_prefix='', initializer=None, initargs=()) 1497db96d56Sopenharmony_ci 1507db96d56Sopenharmony_ci An :class:`Executor` subclass that uses a pool of at most *max_workers* 1517db96d56Sopenharmony_ci threads to execute calls asynchronously. 1527db96d56Sopenharmony_ci 1537db96d56Sopenharmony_ci All threads enqueued to ``ThreadPoolExecutor`` will be joined before the 1547db96d56Sopenharmony_ci interpreter can exit. Note that the exit handler which does this is 1557db96d56Sopenharmony_ci executed *before* any exit handlers added using ``atexit``. This means 1567db96d56Sopenharmony_ci exceptions in the main thread must be caught and handled in order to 1577db96d56Sopenharmony_ci signal threads to exit gracefully. For this reason, it is recommended 1587db96d56Sopenharmony_ci that ``ThreadPoolExecutor`` not be used for long-running tasks. 1597db96d56Sopenharmony_ci 1607db96d56Sopenharmony_ci *initializer* is an optional callable that is called at the start of 1617db96d56Sopenharmony_ci each worker thread; *initargs* is a tuple of arguments passed to the 1627db96d56Sopenharmony_ci initializer. Should *initializer* raise an exception, all currently 1637db96d56Sopenharmony_ci pending jobs will raise a :exc:`~concurrent.futures.thread.BrokenThreadPool`, 1647db96d56Sopenharmony_ci as well as any attempt to submit more jobs to the pool. 1657db96d56Sopenharmony_ci 1667db96d56Sopenharmony_ci .. versionchanged:: 3.5 1677db96d56Sopenharmony_ci If *max_workers* is ``None`` or 1687db96d56Sopenharmony_ci not given, it will default to the number of processors on the machine, 1697db96d56Sopenharmony_ci multiplied by ``5``, assuming that :class:`ThreadPoolExecutor` is often 1707db96d56Sopenharmony_ci used to overlap I/O instead of CPU work and the number of workers 1717db96d56Sopenharmony_ci should be higher than the number of workers 1727db96d56Sopenharmony_ci for :class:`ProcessPoolExecutor`. 1737db96d56Sopenharmony_ci 1747db96d56Sopenharmony_ci .. versionadded:: 3.6 1757db96d56Sopenharmony_ci The *thread_name_prefix* argument was added to allow users to 1767db96d56Sopenharmony_ci control the :class:`threading.Thread` names for worker threads created by 1777db96d56Sopenharmony_ci the pool for easier debugging. 1787db96d56Sopenharmony_ci 1797db96d56Sopenharmony_ci .. versionchanged:: 3.7 1807db96d56Sopenharmony_ci Added the *initializer* and *initargs* arguments. 1817db96d56Sopenharmony_ci 1827db96d56Sopenharmony_ci .. versionchanged:: 3.8 1837db96d56Sopenharmony_ci Default value of *max_workers* is changed to ``min(32, os.cpu_count() + 4)``. 1847db96d56Sopenharmony_ci This default value preserves at least 5 workers for I/O bound tasks. 1857db96d56Sopenharmony_ci It utilizes at most 32 CPU cores for CPU bound tasks which release the GIL. 1867db96d56Sopenharmony_ci And it avoids using very large resources implicitly on many-core machines. 1877db96d56Sopenharmony_ci 1887db96d56Sopenharmony_ci ThreadPoolExecutor now reuses idle worker threads before starting 1897db96d56Sopenharmony_ci *max_workers* worker threads too. 1907db96d56Sopenharmony_ci 1917db96d56Sopenharmony_ci 1927db96d56Sopenharmony_ci.. _threadpoolexecutor-example: 1937db96d56Sopenharmony_ci 1947db96d56Sopenharmony_ciThreadPoolExecutor Example 1957db96d56Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~ 1967db96d56Sopenharmony_ci:: 1977db96d56Sopenharmony_ci 1987db96d56Sopenharmony_ci import concurrent.futures 1997db96d56Sopenharmony_ci import urllib.request 2007db96d56Sopenharmony_ci 2017db96d56Sopenharmony_ci URLS = ['http://www.foxnews.com/', 2027db96d56Sopenharmony_ci 'http://www.cnn.com/', 2037db96d56Sopenharmony_ci 'http://europe.wsj.com/', 2047db96d56Sopenharmony_ci 'http://www.bbc.co.uk/', 2057db96d56Sopenharmony_ci 'http://nonexistant-subdomain.python.org/'] 2067db96d56Sopenharmony_ci 2077db96d56Sopenharmony_ci # Retrieve a single page and report the URL and contents 2087db96d56Sopenharmony_ci def load_url(url, timeout): 2097db96d56Sopenharmony_ci with urllib.request.urlopen(url, timeout=timeout) as conn: 2107db96d56Sopenharmony_ci return conn.read() 2117db96d56Sopenharmony_ci 2127db96d56Sopenharmony_ci # We can use a with statement to ensure threads are cleaned up promptly 2137db96d56Sopenharmony_ci with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: 2147db96d56Sopenharmony_ci # Start the load operations and mark each future with its URL 2157db96d56Sopenharmony_ci future_to_url = {executor.submit(load_url, url, 60): url for url in URLS} 2167db96d56Sopenharmony_ci for future in concurrent.futures.as_completed(future_to_url): 2177db96d56Sopenharmony_ci url = future_to_url[future] 2187db96d56Sopenharmony_ci try: 2197db96d56Sopenharmony_ci data = future.result() 2207db96d56Sopenharmony_ci except Exception as exc: 2217db96d56Sopenharmony_ci print('%r generated an exception: %s' % (url, exc)) 2227db96d56Sopenharmony_ci else: 2237db96d56Sopenharmony_ci print('%r page is %d bytes' % (url, len(data))) 2247db96d56Sopenharmony_ci 2257db96d56Sopenharmony_ci 2267db96d56Sopenharmony_ciProcessPoolExecutor 2277db96d56Sopenharmony_ci------------------- 2287db96d56Sopenharmony_ci 2297db96d56Sopenharmony_ciThe :class:`ProcessPoolExecutor` class is an :class:`Executor` subclass that 2307db96d56Sopenharmony_ciuses a pool of processes to execute calls asynchronously. 2317db96d56Sopenharmony_ci:class:`ProcessPoolExecutor` uses the :mod:`multiprocessing` module, which 2327db96d56Sopenharmony_ciallows it to side-step the :term:`Global Interpreter Lock 2337db96d56Sopenharmony_ci<global interpreter lock>` but also means that 2347db96d56Sopenharmony_cionly picklable objects can be executed and returned. 2357db96d56Sopenharmony_ci 2367db96d56Sopenharmony_ciThe ``__main__`` module must be importable by worker subprocesses. This means 2377db96d56Sopenharmony_cithat :class:`ProcessPoolExecutor` will not work in the interactive interpreter. 2387db96d56Sopenharmony_ci 2397db96d56Sopenharmony_ciCalling :class:`Executor` or :class:`Future` methods from a callable submitted 2407db96d56Sopenharmony_cito a :class:`ProcessPoolExecutor` will result in deadlock. 2417db96d56Sopenharmony_ci 2427db96d56Sopenharmony_ci.. class:: ProcessPoolExecutor(max_workers=None, mp_context=None, initializer=None, initargs=(), max_tasks_per_child=None) 2437db96d56Sopenharmony_ci 2447db96d56Sopenharmony_ci An :class:`Executor` subclass that executes calls asynchronously using a pool 2457db96d56Sopenharmony_ci of at most *max_workers* processes. If *max_workers* is ``None`` or not 2467db96d56Sopenharmony_ci given, it will default to the number of processors on the machine. 2477db96d56Sopenharmony_ci If *max_workers* is less than or equal to ``0``, then a :exc:`ValueError` 2487db96d56Sopenharmony_ci will be raised. 2497db96d56Sopenharmony_ci On Windows, *max_workers* must be less than or equal to ``61``. If it is not 2507db96d56Sopenharmony_ci then :exc:`ValueError` will be raised. If *max_workers* is ``None``, then 2517db96d56Sopenharmony_ci the default chosen will be at most ``61``, even if more processors are 2527db96d56Sopenharmony_ci available. 2537db96d56Sopenharmony_ci *mp_context* can be a multiprocessing context or None. It will be used to 2547db96d56Sopenharmony_ci launch the workers. If *mp_context* is ``None`` or not given, the default 2557db96d56Sopenharmony_ci multiprocessing context is used. 2567db96d56Sopenharmony_ci 2577db96d56Sopenharmony_ci *initializer* is an optional callable that is called at the start of 2587db96d56Sopenharmony_ci each worker process; *initargs* is a tuple of arguments passed to the 2597db96d56Sopenharmony_ci initializer. Should *initializer* raise an exception, all currently 2607db96d56Sopenharmony_ci pending jobs will raise a :exc:`~concurrent.futures.process.BrokenProcessPool`, 2617db96d56Sopenharmony_ci as well as any attempt to submit more jobs to the pool. 2627db96d56Sopenharmony_ci 2637db96d56Sopenharmony_ci *max_tasks_per_child* is an optional argument that specifies the maximum 2647db96d56Sopenharmony_ci number of tasks a single process can execute before it will exit and be 2657db96d56Sopenharmony_ci replaced with a fresh worker process. By default *max_tasks_per_child* is 2667db96d56Sopenharmony_ci ``None`` which means worker processes will live as long as the pool. When 2677db96d56Sopenharmony_ci a max is specified, the "spawn" multiprocessing start method will be used by 2687db96d56Sopenharmony_ci default in absence of a *mp_context* parameter. This feature is incompatible 2697db96d56Sopenharmony_ci with the "fork" start method. 2707db96d56Sopenharmony_ci 2717db96d56Sopenharmony_ci .. versionchanged:: 3.3 2727db96d56Sopenharmony_ci When one of the worker processes terminates abruptly, a 2737db96d56Sopenharmony_ci :exc:`BrokenProcessPool` error is now raised. Previously, behaviour 2747db96d56Sopenharmony_ci was undefined but operations on the executor or its futures would often 2757db96d56Sopenharmony_ci freeze or deadlock. 2767db96d56Sopenharmony_ci 2777db96d56Sopenharmony_ci .. versionchanged:: 3.7 2787db96d56Sopenharmony_ci The *mp_context* argument was added to allow users to control the 2797db96d56Sopenharmony_ci start_method for worker processes created by the pool. 2807db96d56Sopenharmony_ci 2817db96d56Sopenharmony_ci Added the *initializer* and *initargs* arguments. 2827db96d56Sopenharmony_ci 2837db96d56Sopenharmony_ci .. versionchanged:: 3.11 2847db96d56Sopenharmony_ci The *max_tasks_per_child* argument was added to allow users to 2857db96d56Sopenharmony_ci control the lifetime of workers in the pool. 2867db96d56Sopenharmony_ci 2877db96d56Sopenharmony_ci 2887db96d56Sopenharmony_ci.. _processpoolexecutor-example: 2897db96d56Sopenharmony_ci 2907db96d56Sopenharmony_ciProcessPoolExecutor Example 2917db96d56Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2927db96d56Sopenharmony_ci:: 2937db96d56Sopenharmony_ci 2947db96d56Sopenharmony_ci import concurrent.futures 2957db96d56Sopenharmony_ci import math 2967db96d56Sopenharmony_ci 2977db96d56Sopenharmony_ci PRIMES = [ 2987db96d56Sopenharmony_ci 112272535095293, 2997db96d56Sopenharmony_ci 112582705942171, 3007db96d56Sopenharmony_ci 112272535095293, 3017db96d56Sopenharmony_ci 115280095190773, 3027db96d56Sopenharmony_ci 115797848077099, 3037db96d56Sopenharmony_ci 1099726899285419] 3047db96d56Sopenharmony_ci 3057db96d56Sopenharmony_ci def is_prime(n): 3067db96d56Sopenharmony_ci if n < 2: 3077db96d56Sopenharmony_ci return False 3087db96d56Sopenharmony_ci if n == 2: 3097db96d56Sopenharmony_ci return True 3107db96d56Sopenharmony_ci if n % 2 == 0: 3117db96d56Sopenharmony_ci return False 3127db96d56Sopenharmony_ci 3137db96d56Sopenharmony_ci sqrt_n = int(math.floor(math.sqrt(n))) 3147db96d56Sopenharmony_ci for i in range(3, sqrt_n + 1, 2): 3157db96d56Sopenharmony_ci if n % i == 0: 3167db96d56Sopenharmony_ci return False 3177db96d56Sopenharmony_ci return True 3187db96d56Sopenharmony_ci 3197db96d56Sopenharmony_ci def main(): 3207db96d56Sopenharmony_ci with concurrent.futures.ProcessPoolExecutor() as executor: 3217db96d56Sopenharmony_ci for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)): 3227db96d56Sopenharmony_ci print('%d is prime: %s' % (number, prime)) 3237db96d56Sopenharmony_ci 3247db96d56Sopenharmony_ci if __name__ == '__main__': 3257db96d56Sopenharmony_ci main() 3267db96d56Sopenharmony_ci 3277db96d56Sopenharmony_ci 3287db96d56Sopenharmony_ciFuture Objects 3297db96d56Sopenharmony_ci-------------- 3307db96d56Sopenharmony_ci 3317db96d56Sopenharmony_ciThe :class:`Future` class encapsulates the asynchronous execution of a callable. 3327db96d56Sopenharmony_ci:class:`Future` instances are created by :meth:`Executor.submit`. 3337db96d56Sopenharmony_ci 3347db96d56Sopenharmony_ci.. class:: Future 3357db96d56Sopenharmony_ci 3367db96d56Sopenharmony_ci Encapsulates the asynchronous execution of a callable. :class:`Future` 3377db96d56Sopenharmony_ci instances are created by :meth:`Executor.submit` and should not be created 3387db96d56Sopenharmony_ci directly except for testing. 3397db96d56Sopenharmony_ci 3407db96d56Sopenharmony_ci .. method:: cancel() 3417db96d56Sopenharmony_ci 3427db96d56Sopenharmony_ci Attempt to cancel the call. If the call is currently being executed or 3437db96d56Sopenharmony_ci finished running and cannot be cancelled then the method will return 3447db96d56Sopenharmony_ci ``False``, otherwise the call will be cancelled and the method will 3457db96d56Sopenharmony_ci return ``True``. 3467db96d56Sopenharmony_ci 3477db96d56Sopenharmony_ci .. method:: cancelled() 3487db96d56Sopenharmony_ci 3497db96d56Sopenharmony_ci Return ``True`` if the call was successfully cancelled. 3507db96d56Sopenharmony_ci 3517db96d56Sopenharmony_ci .. method:: running() 3527db96d56Sopenharmony_ci 3537db96d56Sopenharmony_ci Return ``True`` if the call is currently being executed and cannot be 3547db96d56Sopenharmony_ci cancelled. 3557db96d56Sopenharmony_ci 3567db96d56Sopenharmony_ci .. method:: done() 3577db96d56Sopenharmony_ci 3587db96d56Sopenharmony_ci Return ``True`` if the call was successfully cancelled or finished 3597db96d56Sopenharmony_ci running. 3607db96d56Sopenharmony_ci 3617db96d56Sopenharmony_ci .. method:: result(timeout=None) 3627db96d56Sopenharmony_ci 3637db96d56Sopenharmony_ci Return the value returned by the call. If the call hasn't yet completed 3647db96d56Sopenharmony_ci then this method will wait up to *timeout* seconds. If the call hasn't 3657db96d56Sopenharmony_ci completed in *timeout* seconds, then a 3667db96d56Sopenharmony_ci :exc:`TimeoutError` will be raised. *timeout* can be 3677db96d56Sopenharmony_ci an int or float. If *timeout* is not specified or ``None``, there is no 3687db96d56Sopenharmony_ci limit to the wait time. 3697db96d56Sopenharmony_ci 3707db96d56Sopenharmony_ci If the future is cancelled before completing then :exc:`.CancelledError` 3717db96d56Sopenharmony_ci will be raised. 3727db96d56Sopenharmony_ci 3737db96d56Sopenharmony_ci If the call raised an exception, this method will raise the same exception. 3747db96d56Sopenharmony_ci 3757db96d56Sopenharmony_ci .. method:: exception(timeout=None) 3767db96d56Sopenharmony_ci 3777db96d56Sopenharmony_ci Return the exception raised by the call. If the call hasn't yet 3787db96d56Sopenharmony_ci completed then this method will wait up to *timeout* seconds. If the 3797db96d56Sopenharmony_ci call hasn't completed in *timeout* seconds, then a 3807db96d56Sopenharmony_ci :exc:`TimeoutError` will be raised. *timeout* can be 3817db96d56Sopenharmony_ci an int or float. If *timeout* is not specified or ``None``, there is no 3827db96d56Sopenharmony_ci limit to the wait time. 3837db96d56Sopenharmony_ci 3847db96d56Sopenharmony_ci If the future is cancelled before completing then :exc:`.CancelledError` 3857db96d56Sopenharmony_ci will be raised. 3867db96d56Sopenharmony_ci 3877db96d56Sopenharmony_ci If the call completed without raising, ``None`` is returned. 3887db96d56Sopenharmony_ci 3897db96d56Sopenharmony_ci .. method:: add_done_callback(fn) 3907db96d56Sopenharmony_ci 3917db96d56Sopenharmony_ci Attaches the callable *fn* to the future. *fn* will be called, with the 3927db96d56Sopenharmony_ci future as its only argument, when the future is cancelled or finishes 3937db96d56Sopenharmony_ci running. 3947db96d56Sopenharmony_ci 3957db96d56Sopenharmony_ci Added callables are called in the order that they were added and are 3967db96d56Sopenharmony_ci always called in a thread belonging to the process that added them. If 3977db96d56Sopenharmony_ci the callable raises an :exc:`Exception` subclass, it will be logged and 3987db96d56Sopenharmony_ci ignored. If the callable raises a :exc:`BaseException` subclass, the 3997db96d56Sopenharmony_ci behavior is undefined. 4007db96d56Sopenharmony_ci 4017db96d56Sopenharmony_ci If the future has already completed or been cancelled, *fn* will be 4027db96d56Sopenharmony_ci called immediately. 4037db96d56Sopenharmony_ci 4047db96d56Sopenharmony_ci The following :class:`Future` methods are meant for use in unit tests and 4057db96d56Sopenharmony_ci :class:`Executor` implementations. 4067db96d56Sopenharmony_ci 4077db96d56Sopenharmony_ci .. method:: set_running_or_notify_cancel() 4087db96d56Sopenharmony_ci 4097db96d56Sopenharmony_ci This method should only be called by :class:`Executor` implementations 4107db96d56Sopenharmony_ci before executing the work associated with the :class:`Future` and by unit 4117db96d56Sopenharmony_ci tests. 4127db96d56Sopenharmony_ci 4137db96d56Sopenharmony_ci If the method returns ``False`` then the :class:`Future` was cancelled, 4147db96d56Sopenharmony_ci i.e. :meth:`Future.cancel` was called and returned ``True``. Any threads 4157db96d56Sopenharmony_ci waiting on the :class:`Future` completing (i.e. through 4167db96d56Sopenharmony_ci :func:`as_completed` or :func:`wait`) will be woken up. 4177db96d56Sopenharmony_ci 4187db96d56Sopenharmony_ci If the method returns ``True`` then the :class:`Future` was not cancelled 4197db96d56Sopenharmony_ci and has been put in the running state, i.e. calls to 4207db96d56Sopenharmony_ci :meth:`Future.running` will return ``True``. 4217db96d56Sopenharmony_ci 4227db96d56Sopenharmony_ci This method can only be called once and cannot be called after 4237db96d56Sopenharmony_ci :meth:`Future.set_result` or :meth:`Future.set_exception` have been 4247db96d56Sopenharmony_ci called. 4257db96d56Sopenharmony_ci 4267db96d56Sopenharmony_ci .. method:: set_result(result) 4277db96d56Sopenharmony_ci 4287db96d56Sopenharmony_ci Sets the result of the work associated with the :class:`Future` to 4297db96d56Sopenharmony_ci *result*. 4307db96d56Sopenharmony_ci 4317db96d56Sopenharmony_ci This method should only be used by :class:`Executor` implementations and 4327db96d56Sopenharmony_ci unit tests. 4337db96d56Sopenharmony_ci 4347db96d56Sopenharmony_ci .. versionchanged:: 3.8 4357db96d56Sopenharmony_ci This method raises 4367db96d56Sopenharmony_ci :exc:`concurrent.futures.InvalidStateError` if the :class:`Future` is 4377db96d56Sopenharmony_ci already done. 4387db96d56Sopenharmony_ci 4397db96d56Sopenharmony_ci .. method:: set_exception(exception) 4407db96d56Sopenharmony_ci 4417db96d56Sopenharmony_ci Sets the result of the work associated with the :class:`Future` to the 4427db96d56Sopenharmony_ci :class:`Exception` *exception*. 4437db96d56Sopenharmony_ci 4447db96d56Sopenharmony_ci This method should only be used by :class:`Executor` implementations and 4457db96d56Sopenharmony_ci unit tests. 4467db96d56Sopenharmony_ci 4477db96d56Sopenharmony_ci .. versionchanged:: 3.8 4487db96d56Sopenharmony_ci This method raises 4497db96d56Sopenharmony_ci :exc:`concurrent.futures.InvalidStateError` if the :class:`Future` is 4507db96d56Sopenharmony_ci already done. 4517db96d56Sopenharmony_ci 4527db96d56Sopenharmony_ciModule Functions 4537db96d56Sopenharmony_ci---------------- 4547db96d56Sopenharmony_ci 4557db96d56Sopenharmony_ci.. function:: wait(fs, timeout=None, return_when=ALL_COMPLETED) 4567db96d56Sopenharmony_ci 4577db96d56Sopenharmony_ci Wait for the :class:`Future` instances (possibly created by different 4587db96d56Sopenharmony_ci :class:`Executor` instances) given by *fs* to complete. Duplicate futures 4597db96d56Sopenharmony_ci given to *fs* are removed and will be returned only once. Returns a named 4607db96d56Sopenharmony_ci 2-tuple of sets. The first set, named ``done``, contains the futures that 4617db96d56Sopenharmony_ci completed (finished or cancelled futures) before the wait completed. The 4627db96d56Sopenharmony_ci second set, named ``not_done``, contains the futures that did not complete 4637db96d56Sopenharmony_ci (pending or running futures). 4647db96d56Sopenharmony_ci 4657db96d56Sopenharmony_ci *timeout* can be used to control the maximum number of seconds to wait before 4667db96d56Sopenharmony_ci returning. *timeout* can be an int or float. If *timeout* is not specified 4677db96d56Sopenharmony_ci or ``None``, there is no limit to the wait time. 4687db96d56Sopenharmony_ci 4697db96d56Sopenharmony_ci *return_when* indicates when this function should return. It must be one of 4707db96d56Sopenharmony_ci the following constants: 4717db96d56Sopenharmony_ci 4727db96d56Sopenharmony_ci .. tabularcolumns:: |l|L| 4737db96d56Sopenharmony_ci 4747db96d56Sopenharmony_ci +-----------------------------+----------------------------------------+ 4757db96d56Sopenharmony_ci | Constant | Description | 4767db96d56Sopenharmony_ci +=============================+========================================+ 4777db96d56Sopenharmony_ci | :const:`FIRST_COMPLETED` | The function will return when any | 4787db96d56Sopenharmony_ci | | future finishes or is cancelled. | 4797db96d56Sopenharmony_ci +-----------------------------+----------------------------------------+ 4807db96d56Sopenharmony_ci | :const:`FIRST_EXCEPTION` | The function will return when any | 4817db96d56Sopenharmony_ci | | future finishes by raising an | 4827db96d56Sopenharmony_ci | | exception. If no future raises an | 4837db96d56Sopenharmony_ci | | exception then it is equivalent to | 4847db96d56Sopenharmony_ci | | :const:`ALL_COMPLETED`. | 4857db96d56Sopenharmony_ci +-----------------------------+----------------------------------------+ 4867db96d56Sopenharmony_ci | :const:`ALL_COMPLETED` | The function will return when all | 4877db96d56Sopenharmony_ci | | futures finish or are cancelled. | 4887db96d56Sopenharmony_ci +-----------------------------+----------------------------------------+ 4897db96d56Sopenharmony_ci 4907db96d56Sopenharmony_ci.. function:: as_completed(fs, timeout=None) 4917db96d56Sopenharmony_ci 4927db96d56Sopenharmony_ci Returns an iterator over the :class:`Future` instances (possibly created by 4937db96d56Sopenharmony_ci different :class:`Executor` instances) given by *fs* that yields futures as 4947db96d56Sopenharmony_ci they complete (finished or cancelled futures). Any futures given by *fs* that 4957db96d56Sopenharmony_ci are duplicated will be returned once. Any futures that completed before 4967db96d56Sopenharmony_ci :func:`as_completed` is called will be yielded first. The returned iterator 4977db96d56Sopenharmony_ci raises a :exc:`TimeoutError` if :meth:`~iterator.__next__` 4987db96d56Sopenharmony_ci is called and the result isn't available after *timeout* seconds from the 4997db96d56Sopenharmony_ci original call to :func:`as_completed`. *timeout* can be an int or float. If 5007db96d56Sopenharmony_ci *timeout* is not specified or ``None``, there is no limit to the wait time. 5017db96d56Sopenharmony_ci 5027db96d56Sopenharmony_ci 5037db96d56Sopenharmony_ci.. seealso:: 5047db96d56Sopenharmony_ci 5057db96d56Sopenharmony_ci :pep:`3148` -- futures - execute computations asynchronously 5067db96d56Sopenharmony_ci The proposal which described this feature for inclusion in the Python 5077db96d56Sopenharmony_ci standard library. 5087db96d56Sopenharmony_ci 5097db96d56Sopenharmony_ci 5107db96d56Sopenharmony_ciException classes 5117db96d56Sopenharmony_ci----------------- 5127db96d56Sopenharmony_ci 5137db96d56Sopenharmony_ci.. currentmodule:: concurrent.futures 5147db96d56Sopenharmony_ci 5157db96d56Sopenharmony_ci.. exception:: CancelledError 5167db96d56Sopenharmony_ci 5177db96d56Sopenharmony_ci Raised when a future is cancelled. 5187db96d56Sopenharmony_ci 5197db96d56Sopenharmony_ci.. exception:: TimeoutError 5207db96d56Sopenharmony_ci 5217db96d56Sopenharmony_ci A deprecated alias of :exc:`TimeoutError`, 5227db96d56Sopenharmony_ci raised when a future operation exceeds the given timeout. 5237db96d56Sopenharmony_ci 5247db96d56Sopenharmony_ci .. versionchanged:: 3.11 5257db96d56Sopenharmony_ci 5267db96d56Sopenharmony_ci This class was made an alias of :exc:`TimeoutError`. 5277db96d56Sopenharmony_ci 5287db96d56Sopenharmony_ci 5297db96d56Sopenharmony_ci.. exception:: BrokenExecutor 5307db96d56Sopenharmony_ci 5317db96d56Sopenharmony_ci Derived from :exc:`RuntimeError`, this exception class is raised 5327db96d56Sopenharmony_ci when an executor is broken for some reason, and cannot be used 5337db96d56Sopenharmony_ci to submit or execute new tasks. 5347db96d56Sopenharmony_ci 5357db96d56Sopenharmony_ci .. versionadded:: 3.7 5367db96d56Sopenharmony_ci 5377db96d56Sopenharmony_ci.. exception:: InvalidStateError 5387db96d56Sopenharmony_ci 5397db96d56Sopenharmony_ci Raised when an operation is performed on a future that is not allowed 5407db96d56Sopenharmony_ci in the current state. 5417db96d56Sopenharmony_ci 5427db96d56Sopenharmony_ci .. versionadded:: 3.8 5437db96d56Sopenharmony_ci 5447db96d56Sopenharmony_ci.. currentmodule:: concurrent.futures.thread 5457db96d56Sopenharmony_ci 5467db96d56Sopenharmony_ci.. exception:: BrokenThreadPool 5477db96d56Sopenharmony_ci 5487db96d56Sopenharmony_ci Derived from :exc:`~concurrent.futures.BrokenExecutor`, this exception 5497db96d56Sopenharmony_ci class is raised when one of the workers of a :class:`ThreadPoolExecutor` 5507db96d56Sopenharmony_ci has failed initializing. 5517db96d56Sopenharmony_ci 5527db96d56Sopenharmony_ci .. versionadded:: 3.7 5537db96d56Sopenharmony_ci 5547db96d56Sopenharmony_ci.. currentmodule:: concurrent.futures.process 5557db96d56Sopenharmony_ci 5567db96d56Sopenharmony_ci.. exception:: BrokenProcessPool 5577db96d56Sopenharmony_ci 5587db96d56Sopenharmony_ci Derived from :exc:`~concurrent.futures.BrokenExecutor` (formerly 5597db96d56Sopenharmony_ci :exc:`RuntimeError`), this exception class is raised when one of the 5607db96d56Sopenharmony_ci workers of a :class:`ProcessPoolExecutor` has terminated in a non-clean 5617db96d56Sopenharmony_ci fashion (for example, if it was killed from the outside). 5627db96d56Sopenharmony_ci 5637db96d56Sopenharmony_ci .. versionadded:: 3.3 564