You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Doc/library/threading.rst
+15-10Lines changed: 15 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -57,16 +57,6 @@ parallel, sharing memory space. Threads are particularly useful when tasks are
57
57
I/O-bound, such as file operations or making network requests,
58
58
where much of the time is spent waiting for external resources.
59
59
60
-
Unlike the :mod:`multiprocessing` module, which uses separate processes to
61
-
bypass the :term:`Global Interpreter Lock <global interpreter lock>`, the
62
-
threading module operates within a single process, meaning that all threads
63
-
share the same memory space. However, the :term:`GIL` limits the performance gains of
64
-
threading when it comes to CPU-bound tasks, as only one thread can execute
65
-
Python bytecode at a time. Despite this, threads remain a useful tool for
66
-
achieving concurrency in many scenarios.
67
-
68
-
As of Python 3.13, experimental :term:`free-threaded <free threading>` builds can disable the GIL, enabling true parallel execution of threads, but this feature is not available by default. (See :pep:`703`.)
69
-
70
60
A typical use case for :mod:`threading` includes managing a pool of worker
71
61
threads that can process multiple tasks concurrently. This basic example of
72
62
creating and starting threads using :class:`~threading.Thread`::
@@ -100,6 +90,21 @@ creating and starting threads using :class:`~threading.Thread`::
100
90
for t in threads:
101
91
t.join()
102
92
93
+
GIL and Performance Considerations
94
+
----------------------------------
95
+
96
+
Unlike the :mod:`multiprocessing` module, which uses separate processes to
97
+
bypass the :term:`Global Interpreter Lock <global interpreter lock>`, the
98
+
threading module operates within a single process, meaning that all threads
99
+
share the same memory space. However, the :term:`GIL` limits the performance
100
+
gains of threading when it comes to CPU-bound tasks, as only one thread can
101
+
execute Python bytecode at a time. Despite this, threads remain a useful tool
102
+
for achieving concurrency in many scenarios.
103
+
104
+
As of Python 3.13, experimental :term:`free-threaded <free threading>` builds
105
+
can disable the :term:`GIL`, enabling true parallel execution of threads, but
106
+
this feature is not available by default. (See :pep:`703`.)
0 commit comments