Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Doc/glossary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"most of its time computing, for example doing mathematical calculations, or string manipulation, or or operating on an in-memory data structure. This keeps CPU core busy."

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer we don't add too many examples, it overloads the definitions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think I'll just use a better example than string sorting. I'll give it a think. At one point this talked about compressing in-memory files but that makes the reader think about I/O which is exactly what this entry isn't about.

CPU core busy. Running such tasks :term:`concurrently <concurrency>` on

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"running multiple such tasks"

one core does not finish the work sooner, because they take turns using
that core, but running them in :term:`parallel <parallelism>` on several
cores can. For example, if sorting a large list takes one second,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorting is maybe not the best example, parallel sorting being non-trivial. Summing a list of integers, maybe?

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Free-threading?

:term:`I/O-bound`.

CPython
The canonical implementation of the Python programming language, as
distributed on `python.org <https://www.python.org>`_. The term "CPython"
Expand Down Expand Up @@ -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 <concurrency>` 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`.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"instead of three when run sequentially."


IDLE
An Integrated Development and Learning Environment for Python.
:ref:`idle` is a basic editor and interpreter environment
Expand Down
3 changes: 2 additions & 1 deletion Doc/library/asyncio-dev.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Doc/library/asyncio-llapi-index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
10 changes: 6 additions & 4 deletions Doc/library/asyncio-task.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use it for CPU-bound tasks too?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I guess that's change of intention so maybe should be different PR.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh but I see it's mentioned later, so it is OK with caveats.

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')}")

Expand Down Expand Up @@ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like free-threading?

:term:`CPU-bound` functions.

.. versionadded:: 3.9

Expand Down
4 changes: 2 additions & 2 deletions Doc/library/asyncio-threading.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, could mention free-threading.


Expand Down
2 changes: 1 addition & 1 deletion Doc/library/asyncio.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand Down
3 changes: 2 additions & 1 deletion Doc/library/concurrency.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
4 changes: 2 additions & 2 deletions Doc/library/concurrent.futures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Doc/library/functools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Doc/library/profiling.sampling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
5 changes: 3 additions & 2 deletions Doc/library/threading.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand Down
Loading