Skip to content

Commit ce23b70

Browse files
miss-islingtonlysnikolaoudpdani
authored
[3.14] gh-140374: Add glossary entries related to multithreading (GH-140375) (#142868)
--------- (cherry picked from commit 1e9a0ee) Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com> Co-authored-by: Daniele Parmeggiani <8658291+dpdani@users.noreply.github.com>
1 parent f2d6f57 commit ce23b70

File tree

1 file changed

+164
-8
lines changed

1 file changed

+164
-8
lines changed

Doc/glossary.rst

Lines changed: 164 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ Glossary
134134
iterator's :meth:`~object.__anext__` method until it raises a
135135
:exc:`StopAsyncIteration` exception. Introduced by :pep:`492`.
136136

137+
atomic operation
138+
An operation that appears to execute as a single, indivisible step: no
139+
other thread can observe it half-done, and its effects become visible all
140+
at once. Python does not guarantee that high-level statements are atomic
141+
(for example, ``x += 1`` performs multiple bytecode operations and is not
142+
atomic). Atomicity is only guaranteed where explicitly documented. See
143+
also :term:`race condition` and :term:`data race`.
144+
137145
attached thread state
138146

139147
A :term:`thread state` that is active for the current OS thread.
@@ -289,6 +297,22 @@ Glossary
289297
advanced mathematical feature. If you're not aware of a need for them,
290298
it's almost certain you can safely ignore them.
291299

300+
concurrency
301+
The ability of a computer program to perform multiple tasks at the same
302+
time. Python provides libraries for writing programs that make use of
303+
different forms of concurrency. :mod:`asyncio` is a library for dealing
304+
with asynchronous tasks and coroutines. :mod:`threading` provides
305+
access to operating system threads and :mod:`multiprocessing` to
306+
operating system processes. Multi-core processors can execute threads and
307+
processes on different CPU cores at the same time (see
308+
:term:`parallelism`).
309+
310+
concurrent modification
311+
When multiple threads modify shared data at the same time. Concurrent
312+
modification without proper synchronization can cause
313+
:term:`race conditions <race condition>`, and might also trigger a
314+
:term:`data race <data race>`, data corruption, or both.
315+
292316
context
293317
This term has different meanings depending on where and how it is used.
294318
Some common meanings:
@@ -363,6 +387,28 @@ Glossary
363387
the :term:`cyclic garbage collector <garbage collection>` is to identify these groups and break the reference
364388
cycles so that the memory can be reclaimed.
365389

390+
data race
391+
A situation where multiple threads access the same memory location
392+
concurrently, at least one of the accesses is a write, and the threads
393+
do not use any synchronization to control their access. Data races
394+
lead to :term:`non-deterministic` behavior and can cause data corruption.
395+
Proper use of :term:`locks <lock>` and other :term:`synchronization primitives
396+
<synchronization primitive>` prevents data races. Note that data races
397+
can only happen in native code, but that :term:`native code` might be
398+
exposed in a Python API. See also :term:`race condition` and
399+
:term:`thread-safe`.
400+
401+
deadlock
402+
A situation in which two or more tasks (threads, processes, or coroutines)
403+
wait indefinitely for each other to release resources or complete actions,
404+
preventing any from making progress. For example, if thread A holds lock
405+
1 and waits for lock 2, while thread B holds lock 2 and waits for lock 1,
406+
both threads will wait indefinitely. In Python this often arises from
407+
acquiring multiple locks in conflicting orders or from circular
408+
join/await dependencies. Deadlocks can be avoided by always acquiring
409+
multiple :term:`locks <lock>` in a consistent order. See also
410+
:term:`lock` and :term:`reentrant`.
411+
366412
decorator
367413
A function returning another function, usually applied as a function
368414
transformation using the ``@wrapper`` syntax. Common examples for
@@ -662,6 +708,14 @@ Glossary
662708
requires the GIL to be held in order to use it. This refers to having an
663709
:term:`attached thread state`.
664710

711+
global state
712+
Data that is accessible throughout a program, such as module-level
713+
variables, class variables, or C static variables in :term:`extension modules
714+
<extension module>`. In multi-threaded programs, global state shared
715+
between threads typically requires synchronization to avoid
716+
:term:`race conditions <race condition>` and
717+
:term:`data races <data race>`.
718+
665719
hash-based pyc
666720
A bytecode cache file that uses the hash rather than the last-modified
667721
time of the corresponding source file to determine its validity. See
@@ -706,7 +760,9 @@ Glossary
706760
tuples. Such an object cannot be altered. A new object has to
707761
be created if a different value has to be stored. They play an important
708762
role in places where a constant hash value is needed, for example as a key
709-
in a dictionary.
763+
in a dictionary. Immutable objects are inherently :term:`thread-safe`
764+
because their state cannot be modified after creation, eliminating concerns
765+
about improperly synchronized :term:`concurrent modification`.
710766

711767
import path
712768
A list of locations (or :term:`path entries <path entry>`) that are
@@ -796,8 +852,9 @@ Glossary
796852

797853
CPython does not consistently apply the requirement that an iterator
798854
define :meth:`~iterator.__iter__`.
799-
And also please note that the free-threading CPython does not guarantee
800-
the thread-safety of iterator operations.
855+
And also please note that :term:`free-threaded <free threading>`
856+
CPython does not guarantee :term:`thread-safe` behavior of iterator
857+
operations.
801858

802859

803860
key function
@@ -835,10 +892,11 @@ Glossary
835892
:keyword:`if` statements.
836893

837894
In a multi-threaded environment, the LBYL approach can risk introducing a
838-
race condition between "the looking" and "the leaping". For example, the
839-
code, ``if key in mapping: return mapping[key]`` can fail if another
895+
:term:`race condition` between "the looking" and "the leaping". For example,
896+
the code, ``if key in mapping: return mapping[key]`` can fail if another
840897
thread removes *key* from *mapping* after the test, but before the lookup.
841-
This issue can be solved with locks or by using the EAFP approach.
898+
This issue can be solved with :term:`locks <lock>` or by using the
899+
:term:`EAFP` approach. See also :term:`thread-safe`.
842900

843901
lexical analyzer
844902

@@ -857,6 +915,19 @@ Glossary
857915
clause is optional. If omitted, all elements in ``range(256)`` are
858916
processed.
859917

918+
lock
919+
A :term:`synchronization primitive` that allows only one thread at a
920+
time to access a shared resource. A thread must acquire a lock before
921+
accessing the protected resource and release it afterward. If a thread
922+
attempts to acquire a lock that is already held by another thread, it
923+
will block until the lock becomes available. Python's :mod:`threading`
924+
module provides :class:`~threading.Lock` (a basic lock) and
925+
:class:`~threading.RLock` (a :term:`reentrant` lock). Locks are used
926+
to prevent :term:`race conditions <race condition>` and ensure
927+
:term:`thread-safe` access to shared data. Alternative design patterns
928+
to locks exist such as queues, producer/consumer patterns, and
929+
thread-local state. See also :term:`deadlock`, and :term:`reentrant`.
930+
860931
loader
861932
An object that loads a module.
862933
It must define the :meth:`!exec_module` and :meth:`!create_module` methods
@@ -942,8 +1013,11 @@ Glossary
9421013
See :term:`method resolution order`.
9431014

9441015
mutable
945-
Mutable objects can change their value but keep their :func:`id`. See
946-
also :term:`immutable`.
1016+
An :term:`object` with state that is allowed to change during the course
1017+
of the program. In multi-threaded programs, mutable objects that are
1018+
shared between threads require careful synchronization to avoid
1019+
:term:`race conditions <race condition>`. See also :term:`immutable`,
1020+
:term:`thread-safe`, and :term:`concurrent modification`.
9471021

9481022
named tuple
9491023
The term "named tuple" applies to any type or class that inherits from
@@ -995,6 +1069,13 @@ Glossary
9951069

9961070
See also :term:`module`.
9971071

1072+
native code
1073+
Code that is compiled to machine instructions and runs directly on the
1074+
processor, as opposed to code that is interpreted or runs in a virtual
1075+
machine. In the context of Python, native code typically refers to
1076+
C, C++, Rust or Fortran code in :term:`extension modules <extension module>`
1077+
that can be called from Python. See also :term:`extension module`.
1078+
9981079
nested scope
9991080
The ability to refer to a variable in an enclosing definition. For
10001081
instance, a function defined inside another function can refer to
@@ -1011,6 +1092,15 @@ Glossary
10111092
properties, :meth:`~object.__getattribute__`, class methods, and static
10121093
methods.
10131094

1095+
non-deterministic
1096+
Behavior where the outcome of a program can vary between executions with
1097+
the same inputs. In multi-threaded programs, non-deterministic behavior
1098+
often results from :term:`race conditions <race condition>` where the
1099+
relative timing or interleaving of threads affects the result.
1100+
Proper synchronization using :term:`locks <lock>` and other
1101+
:term:`synchronization primitives <synchronization primitive>` helps
1102+
ensure deterministic behavior.
1103+
10141104
object
10151105
Any data with state (attributes or value) and defined behavior
10161106
(methods). Also the ultimate base class of any :term:`new-style
@@ -1041,6 +1131,16 @@ Glossary
10411131

10421132
See also :term:`regular package` and :term:`namespace package`.
10431133

1134+
parallelism
1135+
Executing multiple operations at the same time (e.g. on multiple CPU
1136+
cores). In Python builds with the
1137+
:term:`global interpreter lock (GIL) <global interpreter lock>`, only one
1138+
thread runs Python bytecode at a time, so taking advantage of multiple
1139+
CPU cores typically involves multiple processes
1140+
(e.g. :mod:`multiprocessing`) or native extensions that release the GIL.
1141+
In :term:`free-threaded <free threading>` Python, multiple Python threads
1142+
can run Python code simultaneously on different cores.
1143+
10441144
parameter
10451145
A named entity in a :term:`function` (or method) definition that
10461146
specifies an :term:`argument` (or in some cases, arguments) that the
@@ -1215,6 +1315,18 @@ Glossary
12151315
>>> email.mime.text.__name__
12161316
'email.mime.text'
12171317

1318+
race condition
1319+
A condition of a program where the its behavior
1320+
depends on the relative timing or ordering of events, particularly in
1321+
multi-threaded programs. Race conditions can lead to
1322+
:term:`non-deterministic` behavior and bugs that are difficult to
1323+
reproduce. A :term:`data race` is a specific type of race condition
1324+
involving unsynchronized access to shared memory. The :term:`LBYL`
1325+
coding style is particularly susceptible to race conditions in
1326+
multi-threaded code. Using :term:`locks <lock>` and other
1327+
:term:`synchronization primitives <synchronization primitive>`
1328+
helps prevent race conditions.
1329+
12181330
reference count
12191331
The number of references to an object. When the reference count of an
12201332
object drops to zero, it is deallocated. Some objects are
@@ -1236,6 +1348,25 @@ Glossary
12361348

12371349
See also :term:`namespace package`.
12381350

1351+
reentrant
1352+
A property of a function or :term:`lock` that allows it to be called or
1353+
acquired multiple times by the same thread without causing errors or a
1354+
:term:`deadlock`.
1355+
1356+
For functions, reentrancy means the function can be safely called again
1357+
before a previous invocation has completed, which is important when
1358+
functions may be called recursively or from signal handlers. Thread-unsafe
1359+
functions may be :term:`non-deterministic` if they're called reentrantly in a
1360+
multithreaded program.
1361+
1362+
For locks, Python's :class:`threading.RLock` (reentrant lock) is
1363+
reentrant, meaning a thread that already holds the lock can acquire it
1364+
again without blocking. In contrast, :class:`threading.Lock` is not
1365+
reentrant - attempting to acquire it twice from the same thread will cause
1366+
a deadlock.
1367+
1368+
See also :term:`lock` and :term:`deadlock`.
1369+
12391370
REPL
12401371
An acronym for the "read–eval–print loop", another name for the
12411372
:term:`interactive` interpreter shell.
@@ -1340,6 +1471,18 @@ Glossary
13401471

13411472
See also :term:`borrowed reference`.
13421473

1474+
synchronization primitive
1475+
A basic building block for coordinating (synchronizing) the execution of
1476+
multiple threads to ensure :term:`thread-safe` access to shared resources.
1477+
Python's :mod:`threading` module provides several synchronization primitives
1478+
including :class:`~threading.Lock`, :class:`~threading.RLock`,
1479+
:class:`~threading.Semaphore`, :class:`~threading.Condition`,
1480+
:class:`~threading.Event`, and :class:`~threading.Barrier`. Additionally,
1481+
the :mod:`queue` module provides multi-producer, multi-consumer queues
1482+
that are especially useful in multithreaded programs. These
1483+
primitives help prevent :term:`race conditions <race condition>` and
1484+
coordinate thread execution. See also :term:`lock`.
1485+
13431486
t-string
13441487
t-strings
13451488
String literals prefixed with ``t`` or ``T`` are commonly called
@@ -1392,6 +1535,19 @@ Glossary
13921535
See :ref:`Thread State and the Global Interpreter Lock <threads>` for more
13931536
information.
13941537

1538+
thread-safe
1539+
A module, function, or class that behaves correctly when used by multiple
1540+
threads concurrently. Thread-safe code uses appropriate
1541+
:term:`synchronization primitives <synchronization primitive>` like
1542+
:term:`locks <lock>` to protect shared mutable state, or is designed
1543+
to avoid shared mutable state entirely. In the
1544+
:term:`free-threaded <free threading>` build, built-in types like
1545+
:class:`dict`, :class:`list`, and :class:`set` use internal locking
1546+
to make many operations thread-safe, although thread safety is not
1547+
necessarily guaranteed. Code that is not thread-safe may experience
1548+
:term:`race conditions <race condition>` and :term:`data races <data race>`
1549+
when used in multi-threaded programs.
1550+
13951551
token
13961552

13971553
A small unit of source code, generated by the

0 commit comments

Comments
 (0)