diff --git a/Doc/glossary.rst b/Doc/glossary.rst index 8062d2b916dd06..345b9690086b25 100644 --- a/Doc/glossary.rst +++ b/Doc/glossary.rst @@ -372,6 +372,18 @@ Glossary :keyword:`async with` keywords. These were introduced by :pep:`492`. + CPU-bound + Describes a task that spends most of its time computing, which keeps a + CPU core busy. Running such tasks :term:`concurrently ` on + one core does not finish the work sooner, because they take turns using + that core, but running them in :term:`parallel ` on several + cores can. For example, if sorting a large list takes one second, + sorting three of them takes about three seconds on one core and can take + as little as one second on three cores. With the + :term:`global interpreter lock`, only one thread runs Python bytecode at + a time, so threads do not speed up CPU-bound Python code. See also + :term:`I/O-bound`. + CPython The canonical implementation of the Python programming language, as distributed on `python.org `_. The term "CPython" @@ -767,6 +779,15 @@ Glossary compare unequal (except with themselves), and their hash value is derived from their :func:`id`. + I/O-bound + Describes a task that spends most of its time waiting for input or + output, such as a network response or a disk read, and little of it using + the CPU. While one I/O-bound task waits, the program can work on + another, so running such tasks :term:`concurrently ` lets + their waits overlap, even on a single CPU core. For example, three + downloads that each take one second can finish in about one second when + run concurrently, instead of three. See also :term:`CPU-bound`. + IDLE An Integrated Development and Learning Environment for Python. :ref:`idle` is a basic editor and interpreter environment diff --git a/Doc/library/asyncio-dev.rst b/Doc/library/asyncio-dev.rst index 713b40d746680a..85eac4162765ef 100644 --- a/Doc/library/asyncio-dev.rst +++ b/Doc/library/asyncio-dev.rst @@ -120,7 +120,8 @@ code in a different process. Running Blocking Code ===================== -Blocking (CPU-bound) code should not be called directly. For example, +Blocking code, whether :term:`CPU-bound` or :term:`I/O-bound`, should not +be called directly from the event loop thread. For example, if a function performs a CPU-intensive calculation for 1 second, all concurrent asyncio Tasks and IO operations would be delayed by 1 second. diff --git a/Doc/library/asyncio-llapi-index.rst b/Doc/library/asyncio-llapi-index.rst index faf67209a2b0ef..c600a620025f5e 100644 --- a/Doc/library/asyncio-llapi-index.rst +++ b/Doc/library/asyncio-llapi-index.rst @@ -102,7 +102,7 @@ See also the main documentation section about the :class: full-width-table * - ``await`` :meth:`loop.run_in_executor` - - Run a CPU-bound or other blocking function in + - Run a :term:`CPU-bound` or other blocking function in a :mod:`concurrent.futures` executor. * - :meth:`loop.set_default_executor` diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst index bc79927b985627..95bc0e15e665a1 100644 --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@ -1061,13 +1061,14 @@ Running in threads Return a coroutine that can be awaited to get the eventual result of *func*. This coroutine function is primarily intended to be used for executing - IO-bound functions/methods that would otherwise block the event loop if + :term:`I/O-bound` functions/methods that would otherwise block + the event loop if they were run in the main thread. For example:: def blocking_io(): print(f"start blocking_io at {time.strftime('%X')}") # Note that time.sleep() can be replaced with any blocking - # IO-bound operation, such as file operations. + # I/O-bound operation, such as file operations. time.sleep(1) print(f"blocking_io complete at {time.strftime('%X')}") @@ -1098,9 +1099,10 @@ Running in threads .. note:: Due to the :term:`GIL`, ``asyncio.to_thread()`` can typically only be used - to make IO-bound functions non-blocking. However, for extension modules + to make I/O-bound functions non-blocking. However, for extension modules that release the GIL or alternative Python implementations that don't - have one, ``asyncio.to_thread()`` can also be used for CPU-bound functions. + have one, ``asyncio.to_thread()`` can also be used for + :term:`CPU-bound` functions. .. versionadded:: 3.9 diff --git a/Doc/library/asyncio-threading.rst b/Doc/library/asyncio-threading.rst index 526901a2e7eb20..9f1ad9f1e3d91b 100644 --- a/Doc/library/asyncio-threading.rst +++ b/Doc/library/asyncio-threading.rst @@ -7,8 +7,8 @@ asyncio and free-threaded Python asyncio uses an event loop as a scheduler to enable highly efficient concurrency by switching between tasks to allow non-blocking I/O -operations. This results in better performance for I/O-bound use -cases. It also allows off-loading CPU-bound work to a thread or +operations. This results in better performance for :term:`I/O-bound` use +cases. It also allows off-loading :term:`CPU-bound` work to a thread or process pool, but that is still limited by the :term:`global interpreter lock` in CPython. diff --git a/Doc/library/asyncio.rst b/Doc/library/asyncio.rst index 956b00f0873a0d..0759adc18b2a57 100644 --- a/Doc/library/asyncio.rst +++ b/Doc/library/asyncio.rst @@ -26,7 +26,7 @@ asyncio is used as a foundation for multiple Python asynchronous frameworks that provide high-performance network and web-servers, database connection libraries, distributed task queues, etc. -asyncio is often a perfect fit for IO-bound and high-level +asyncio is often a perfect fit for :term:`I/O-bound` and high-level **structured** network code. .. seealso:: diff --git a/Doc/library/concurrency.rst b/Doc/library/concurrency.rst index 18f9443cbfea20..fb207697d48c78 100644 --- a/Doc/library/concurrency.rst +++ b/Doc/library/concurrency.rst @@ -6,7 +6,8 @@ Concurrent Execution The modules described in this chapter provide support for concurrent execution of code. The appropriate choice of tool will depend on the -task to be executed (CPU bound vs IO bound) and preferred style of +task to be executed (:term:`CPU-bound` vs :term:`I/O-bound`) and +preferred style of development (event driven cooperative multitasking vs preemptive multitasking). Here's an overview: diff --git a/Doc/library/concurrent.futures.rst b/Doc/library/concurrent.futures.rst index 77d57509964bd6..2980f614e10549 100644 --- a/Doc/library/concurrent.futures.rst +++ b/Doc/library/concurrent.futures.rst @@ -221,8 +221,8 @@ And:: .. versionchanged:: 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. + This default value preserves at least 5 workers for :term:`I/O-bound` tasks. + It utilizes at most 32 CPU cores for :term:`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 starting diff --git a/Doc/library/functools.rst b/Doc/library/functools.rst index 2ce07976c39531..3433f668b7e8d0 100644 --- a/Doc/library/functools.rst +++ b/Doc/library/functools.rst @@ -149,7 +149,8 @@ The :mod:`!functools` module defines the following functions: lru_cache(maxsize=128, typed=False) Decorator to wrap a function with a memoizing callable that saves up to the - *maxsize* most recent calls. It can save time when an expensive or I/O bound + *maxsize* most recent calls. It can save time when an + expensive or :term:`I/O-bound` function is periodically called with the same arguments. The cache is threadsafe so that the wrapped function can be used in diff --git a/Doc/library/profiling.sampling.rst b/Doc/library/profiling.sampling.rst index b4dfd344299f4a..835b665e1546b8 100644 --- a/Doc/library/profiling.sampling.rst +++ b/Doc/library/profiling.sampling.rst @@ -779,7 +779,8 @@ hotspot---actively using the CPU. Optimization should focus on algorithmic improvements or more efficient code. If a function is high in wall-clock mode but low or absent in CPU mode, it is -I/O-bound or waiting. The function spends most of its time waiting for network, +:term:`I/O-bound` or waiting. The function spends most of its time +waiting for network, disk, locks, or sleep. CPU optimization won't help here; consider async I/O, connection pooling, or reducing wait time instead. diff --git a/Doc/library/threading.rst b/Doc/library/threading.rst index 5e80c30953955d..28c5f17364c684 100644 --- a/Doc/library/threading.rst +++ b/Doc/library/threading.rst @@ -21,7 +21,7 @@ The :mod:`!threading` module provides a way to run multiple `threads units of a process) concurrently within a single process. It allows for the creation and management of threads, making it possible to execute tasks in parallel, sharing memory space. Threads are particularly useful when tasks are -I/O bound, such as file operations or making network requests, +:term:`I/O-bound`, such as file operations or making network requests, where much of the time is spent waiting for external resources. A typical use case for :mod:`!threading` includes managing a pool of worker @@ -98,7 +98,8 @@ Unlike the :mod:`multiprocessing` module, which uses separate processes to bypass the :term:`global interpreter lock` (GIL), the threading module operates within a single process, meaning that all threads share the same memory space. However, the GIL limits the performance gains of threading when it comes to -CPU-bound tasks, as only one thread can execute Python bytecode at a time. +:term:`CPU-bound` tasks, as only one thread can execute Python bytecode +at a time. Despite this, threads remain a useful tool for achieving concurrency in many scenarios.