Skip to content

Commit f631804

Browse files
lysnikolaoudpdani
andcommitted
[3.13] gh-140374: Add glossary entries related to multithreading (GH-140375)
--------- (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 e4ca817 commit f631804

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
@@ -129,6 +129,14 @@ Glossary
129129
iterator's :meth:`~object.__anext__` method until it raises a
130130
:exc:`StopAsyncIteration` exception. Introduced by :pep:`492`.
131131

132+
atomic operation
133+
An operation that appears to execute as a single, indivisible step: no
134+
other thread can observe it half-done, and its effects become visible all
135+
at once. Python does not guarantee that high-level statements are atomic
136+
(for example, ``x += 1`` performs multiple bytecode operations and is not
137+
atomic). Atomicity is only guaranteed where explicitly documented. See
138+
also :term:`race condition` and :term:`data race`.
139+
132140
attribute
133141
A value associated with an object which is usually referenced by name
134142
using dotted expressions.
@@ -262,6 +270,22 @@ Glossary
262270
advanced mathematical feature. If you're not aware of a need for them,
263271
it's almost certain you can safely ignore them.
264272

273+
concurrency
274+
The ability of a computer program to perform multiple tasks at the same
275+
time. Python provides libraries for writing programs that make use of
276+
different forms of concurrency. :mod:`asyncio` is a library for dealing
277+
with asynchronous tasks and coroutines. :mod:`threading` provides
278+
access to operating system threads and :mod:`multiprocessing` to
279+
operating system processes. Multi-core processors can execute threads and
280+
processes on different CPU cores at the same time (see
281+
:term:`parallelism`).
282+
283+
concurrent modification
284+
When multiple threads modify shared data at the same time. Concurrent
285+
modification without proper synchronization can cause
286+
:term:`race conditions <race condition>`, and might also trigger a
287+
:term:`data race <data race>`, data corruption, or both.
288+
265289
context
266290
This term has different meanings depending on where and how it is used.
267291
Some common meanings:
@@ -330,6 +354,28 @@ Glossary
330354
tasks (see :mod:`asyncio`) associate each task with a context which
331355
becomes the current context whenever the task starts or resumes execution.
332356

357+
data race
358+
A situation where multiple threads access the same memory location
359+
concurrently, at least one of the accesses is a write, and the threads
360+
do not use any synchronization to control their access. Data races
361+
lead to :term:`non-deterministic` behavior and can cause data corruption.
362+
Proper use of :term:`locks <lock>` and other :term:`synchronization primitives
363+
<synchronization primitive>` prevents data races. Note that data races
364+
can only happen in native code, but that :term:`native code` might be
365+
exposed in a Python API. See also :term:`race condition` and
366+
:term:`thread-safe`.
367+
368+
deadlock
369+
A situation in which two or more tasks (threads, processes, or coroutines)
370+
wait indefinitely for each other to release resources or complete actions,
371+
preventing any from making progress. For example, if thread A holds lock
372+
1 and waits for lock 2, while thread B holds lock 2 and waits for lock 1,
373+
both threads will wait indefinitely. In Python this often arises from
374+
acquiring multiple locks in conflicting orders or from circular
375+
join/await dependencies. Deadlocks can be avoided by always acquiring
376+
multiple :term:`locks <lock>` in a consistent order. See also
377+
:term:`lock` and :term:`reentrant`.
378+
333379
decorator
334380
A function returning another function, usually applied as a function
335381
transformation using the ``@wrapper`` syntax. Common examples for
@@ -619,6 +665,14 @@ Glossary
619665
multi-threaded applications and makes it easier to use multi-core CPUs
620666
efficiently. For more details, see :pep:`703`.
621667

668+
global state
669+
Data that is accessible throughout a program, such as module-level
670+
variables, class variables, or C static variables in :term:`extension modules
671+
<extension module>`. In multi-threaded programs, global state shared
672+
between threads typically requires synchronization to avoid
673+
:term:`race conditions <race condition>` and
674+
:term:`data races <data race>`.
675+
622676
hash-based pyc
623677
A bytecode cache file that uses the hash rather than the last-modified
624678
time of the corresponding source file to determine its validity. See
@@ -660,7 +714,9 @@ Glossary
660714
tuples. Such an object cannot be altered. A new object has to
661715
be created if a different value has to be stored. They play an important
662716
role in places where a constant hash value is needed, for example as a key
663-
in a dictionary.
717+
in a dictionary. Immutable objects are inherently :term:`thread-safe`
718+
because their state cannot be modified after creation, eliminating concerns
719+
about improperly synchronized :term:`concurrent modification`.
664720

665721
import path
666722
A list of locations (or :term:`path entries <path entry>`) that are
@@ -750,8 +806,9 @@ Glossary
750806

751807
CPython does not consistently apply the requirement that an iterator
752808
define :meth:`~iterator.__iter__`.
753-
And also please note that the free-threading CPython does not guarantee
754-
the thread-safety of iterator operations.
809+
And also please note that :term:`free-threaded <free threading>`
810+
CPython does not guarantee :term:`thread-safe` behavior of iterator
811+
operations.
755812

756813

757814
key function
@@ -789,10 +846,11 @@ Glossary
789846
:keyword:`if` statements.
790847

791848
In a multi-threaded environment, the LBYL approach can risk introducing a
792-
race condition between "the looking" and "the leaping". For example, the
793-
code, ``if key in mapping: return mapping[key]`` can fail if another
849+
:term:`race condition` between "the looking" and "the leaping". For example,
850+
the code, ``if key in mapping: return mapping[key]`` can fail if another
794851
thread removes *key* from *mapping* after the test, but before the lookup.
795-
This issue can be solved with locks or by using the EAFP approach.
852+
This issue can be solved with :term:`locks <lock>` or by using the
853+
:term:`EAFP` approach. See also :term:`thread-safe`.
796854

797855
lexical analyzer
798856

@@ -811,6 +869,19 @@ Glossary
811869
clause is optional. If omitted, all elements in ``range(256)`` are
812870
processed.
813871

872+
lock
873+
A :term:`synchronization primitive` that allows only one thread at a
874+
time to access a shared resource. A thread must acquire a lock before
875+
accessing the protected resource and release it afterward. If a thread
876+
attempts to acquire a lock that is already held by another thread, it
877+
will block until the lock becomes available. Python's :mod:`threading`
878+
module provides :class:`~threading.Lock` (a basic lock) and
879+
:class:`~threading.RLock` (a :term:`reentrant` lock). Locks are used
880+
to prevent :term:`race conditions <race condition>` and ensure
881+
:term:`thread-safe` access to shared data. Alternative design patterns
882+
to locks exist such as queues, producer/consumer patterns, and
883+
thread-local state. See also :term:`deadlock`, and :term:`reentrant`.
884+
814885
loader
815886
An object that loads a module.
816887
It must define the :meth:`!exec_module` and :meth:`!create_module` methods
@@ -896,8 +967,11 @@ Glossary
896967
See :term:`method resolution order`.
897968

898969
mutable
899-
Mutable objects can change their value but keep their :func:`id`. See
900-
also :term:`immutable`.
970+
An :term:`object` with state that is allowed to change during the course
971+
of the program. In multi-threaded programs, mutable objects that are
972+
shared between threads require careful synchronization to avoid
973+
:term:`race conditions <race condition>`. See also :term:`immutable`,
974+
:term:`thread-safe`, and :term:`concurrent modification`.
901975

902976
named tuple
903977
The term "named tuple" applies to any type or class that inherits from
@@ -949,6 +1023,13 @@ Glossary
9491023

9501024
See also :term:`module`.
9511025

1026+
native code
1027+
Code that is compiled to machine instructions and runs directly on the
1028+
processor, as opposed to code that is interpreted or runs in a virtual
1029+
machine. In the context of Python, native code typically refers to
1030+
C, C++, Rust or Fortran code in :term:`extension modules <extension module>`
1031+
that can be called from Python. See also :term:`extension module`.
1032+
9521033
nested scope
9531034
The ability to refer to a variable in an enclosing definition. For
9541035
instance, a function defined inside another function can refer to
@@ -965,6 +1046,15 @@ Glossary
9651046
properties, :meth:`~object.__getattribute__`, class methods, and static
9661047
methods.
9671048

1049+
non-deterministic
1050+
Behavior where the outcome of a program can vary between executions with
1051+
the same inputs. In multi-threaded programs, non-deterministic behavior
1052+
often results from :term:`race conditions <race condition>` where the
1053+
relative timing or interleaving of threads affects the result.
1054+
Proper synchronization using :term:`locks <lock>` and other
1055+
:term:`synchronization primitives <synchronization primitive>` helps
1056+
ensure deterministic behavior.
1057+
9681058
object
9691059
Any data with state (attributes or value) and defined behavior
9701060
(methods). Also the ultimate base class of any :term:`new-style
@@ -986,6 +1076,16 @@ Glossary
9861076

9871077
See also :term:`regular package` and :term:`namespace package`.
9881078

1079+
parallelism
1080+
Executing multiple operations at the same time (e.g. on multiple CPU
1081+
cores). In Python builds with the
1082+
:term:`global interpreter lock (GIL) <global interpreter lock>`, only one
1083+
thread runs Python bytecode at a time, so taking advantage of multiple
1084+
CPU cores typically involves multiple processes
1085+
(e.g. :mod:`multiprocessing`) or native extensions that release the GIL.
1086+
In :term:`free-threaded <free threading>` Python, multiple Python threads
1087+
can run Python code simultaneously on different cores.
1088+
9891089
parameter
9901090
A named entity in a :term:`function` (or method) definition that
9911091
specifies an :term:`argument` (or in some cases, arguments) that the
@@ -1160,6 +1260,18 @@ Glossary
11601260
>>> email.mime.text.__name__
11611261
'email.mime.text'
11621262

1263+
race condition
1264+
A condition of a program where the its behavior
1265+
depends on the relative timing or ordering of events, particularly in
1266+
multi-threaded programs. Race conditions can lead to
1267+
:term:`non-deterministic` behavior and bugs that are difficult to
1268+
reproduce. A :term:`data race` is a specific type of race condition
1269+
involving unsynchronized access to shared memory. The :term:`LBYL`
1270+
coding style is particularly susceptible to race conditions in
1271+
multi-threaded code. Using :term:`locks <lock>` and other
1272+
:term:`synchronization primitives <synchronization primitive>`
1273+
helps prevent race conditions.
1274+
11631275
reference count
11641276
The number of references to an object. When the reference count of an
11651277
object drops to zero, it is deallocated. Some objects are
@@ -1181,6 +1293,25 @@ Glossary
11811293

11821294
See also :term:`namespace package`.
11831295

1296+
reentrant
1297+
A property of a function or :term:`lock` that allows it to be called or
1298+
acquired multiple times by the same thread without causing errors or a
1299+
:term:`deadlock`.
1300+
1301+
For functions, reentrancy means the function can be safely called again
1302+
before a previous invocation has completed, which is important when
1303+
functions may be called recursively or from signal handlers. Thread-unsafe
1304+
functions may be :term:`non-deterministic` if they're called reentrantly in a
1305+
multithreaded program.
1306+
1307+
For locks, Python's :class:`threading.RLock` (reentrant lock) is
1308+
reentrant, meaning a thread that already holds the lock can acquire it
1309+
again without blocking. In contrast, :class:`threading.Lock` is not
1310+
reentrant - attempting to acquire it twice from the same thread will cause
1311+
a deadlock.
1312+
1313+
See also :term:`lock` and :term:`deadlock`.
1314+
11841315
REPL
11851316
An acronym for the "read–eval–print loop", another name for the
11861317
:term:`interactive` interpreter shell.
@@ -1285,6 +1416,18 @@ Glossary
12851416

12861417
See also :term:`borrowed reference`.
12871418

1419+
synchronization primitive
1420+
A basic building block for coordinating (synchronizing) the execution of
1421+
multiple threads to ensure :term:`thread-safe` access to shared resources.
1422+
Python's :mod:`threading` module provides several synchronization primitives
1423+
including :class:`~threading.Lock`, :class:`~threading.RLock`,
1424+
:class:`~threading.Semaphore`, :class:`~threading.Condition`,
1425+
:class:`~threading.Event`, and :class:`~threading.Barrier`. Additionally,
1426+
the :mod:`queue` module provides multi-producer, multi-consumer queues
1427+
that are especially useful in multithreaded programs. These
1428+
primitives help prevent :term:`race conditions <race condition>` and
1429+
coordinate thread execution. See also :term:`lock`.
1430+
12881431
text encoding
12891432
A string in Python is a sequence of Unicode code points (in range
12901433
``U+0000``--``U+10FFFF``). To store or transfer a string, it needs to be
@@ -1308,6 +1451,19 @@ Glossary
13081451
See also :term:`binary file` for a file object able to read and write
13091452
:term:`bytes-like objects <bytes-like object>`.
13101453

1454+
thread-safe
1455+
A module, function, or class that behaves correctly when used by multiple
1456+
threads concurrently. Thread-safe code uses appropriate
1457+
:term:`synchronization primitives <synchronization primitive>` like
1458+
:term:`locks <lock>` to protect shared mutable state, or is designed
1459+
to avoid shared mutable state entirely. In the
1460+
:term:`free-threaded <free threading>` build, built-in types like
1461+
:class:`dict`, :class:`list`, and :class:`set` use internal locking
1462+
to make many operations thread-safe, although thread safety is not
1463+
necessarily guaranteed. Code that is not thread-safe may experience
1464+
:term:`race conditions <race condition>` and :term:`data races <data race>`
1465+
when used in multi-threaded programs.
1466+
13111467
token
13121468

13131469
A small unit of source code, generated by the

0 commit comments

Comments
 (0)