aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Doc/library/threading.rst
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/library/threading.rst')
-rw-r--r--Doc/library/threading.rst245
1 files changed, 209 insertions, 36 deletions
diff --git a/Doc/library/threading.rst b/Doc/library/threading.rst
index d948493c210..cabb41442f8 100644
--- a/Doc/library/threading.rst
+++ b/Doc/library/threading.rst
@@ -11,6 +11,52 @@
This module constructs higher-level threading interfaces on top of the lower
level :mod:`_thread` module.
+.. include:: ../includes/wasm-notavail.rst
+
+Introduction
+------------
+
+The :mod:`!threading` module provides a way to run multiple `threads
+<https://en.wikipedia.org/wiki/Thread_(computing)>`_ (smaller
+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,
+where much of the time is spent waiting for external resources.
+
+A typical use case for :mod:`!threading` includes managing a pool of worker
+threads that can process multiple tasks concurrently. Here's a basic example of
+creating and starting threads using :class:`~threading.Thread`::
+
+ import threading
+ import time
+
+ def crawl(link, delay=3):
+ print(f"crawl started for {link}")
+ time.sleep(delay) # Blocking I/O (simulating a network request)
+ print(f"crawl ended for {link}")
+
+ links = [
+ "https://python.org",
+ "https://docs.python.org",
+ "https://peps.python.org",
+ ]
+
+ # Start threads for each link
+ threads = []
+ for link in links:
+ # Using `args` to pass positional arguments and `kwargs` for keyword arguments
+ t = threading.Thread(target=crawl, args=(link,), kwargs={"delay": 2})
+ threads.append(t)
+
+ # Start each thread
+ for t in threads:
+ t.start()
+
+ # Wait for all threads to finish
+ for t in threads:
+ t.join()
+
.. versionchanged:: 3.7
This module used to be optional, it is now always available.
@@ -45,7 +91,25 @@ level :mod:`_thread` module.
However, threading is still an appropriate model if you want to run
multiple I/O-bound tasks simultaneously.
-.. include:: ../includes/wasm-notavail.rst
+GIL and performance considerations
+----------------------------------
+
+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.
+Despite this, threads remain a useful tool for achieving concurrency in many
+scenarios.
+
+As of Python 3.13, :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`).
+
+.. TODO: At some point this feature will become available by default.
+
+Reference
+---------
This module defines the following functions:
@@ -62,7 +126,7 @@ This module defines the following functions:
Return the current :class:`Thread` object, corresponding to the caller's thread
of control. If the caller's thread of control was not created through the
- :mod:`threading` module, a dummy thread object with limited functionality is
+ :mod:`!threading` module, a dummy thread object with limited functionality is
returned.
The function ``currentThread`` is a deprecated alias for this function.
@@ -157,13 +221,13 @@ This module defines the following functions:
.. index:: single: trace function
- Set a trace function for all threads started from the :mod:`threading` module.
+ Set a trace function for all threads started from the :mod:`!threading` module.
The *func* will be passed to :func:`sys.settrace` for each thread, before its
:meth:`~Thread.run` method is called.
.. function:: settrace_all_threads(func)
- Set a trace function for all threads started from the :mod:`threading` module
+ Set a trace function for all threads started from the :mod:`!threading` module
and all Python threads that are currently executing.
The *func* will be passed to :func:`sys.settrace` for each thread, before its
@@ -186,13 +250,13 @@ This module defines the following functions:
.. index:: single: profile function
- Set a profile function for all threads started from the :mod:`threading` module.
+ Set a profile function for all threads started from the :mod:`!threading` module.
The *func* will be passed to :func:`sys.setprofile` for each thread, before its
:meth:`~Thread.run` method is called.
.. function:: setprofile_all_threads(func)
- Set a profile function for all threads started from the :mod:`threading` module
+ Set a profile function for all threads started from the :mod:`!threading` module
and all Python threads that are currently executing.
The *func* will be passed to :func:`sys.setprofile` for each thread, before its
@@ -257,31 +321,140 @@ when implemented, are mapped to module-level functions.
All of the methods described below are executed atomically.
-Thread-Local Data
------------------
+Thread-local data
+^^^^^^^^^^^^^^^^^
+
+Thread-local data is data whose values are thread specific. If you
+have data that you want to be local to a thread, create a
+:class:`local` object and use its attributes::
+
+ >>> mydata = local()
+ >>> mydata.number = 42
+ >>> mydata.number
+ 42
+
+You can also access the :class:`local`-object's dictionary::
+
+ >>> mydata.__dict__
+ {'number': 42}
+ >>> mydata.__dict__.setdefault('widgets', [])
+ []
+ >>> mydata.widgets
+ []
+
+If we access the data in a different thread::
+
+ >>> log = []
+ >>> def f():
+ ... items = sorted(mydata.__dict__.items())
+ ... log.append(items)
+ ... mydata.number = 11
+ ... log.append(mydata.number)
+
+ >>> import threading
+ >>> thread = threading.Thread(target=f)
+ >>> thread.start()
+ >>> thread.join()
+ >>> log
+ [[], 11]
+
+we get different data. Furthermore, changes made in the other thread
+don't affect data seen in this thread::
+
+ >>> mydata.number
+ 42
+
+Of course, values you get from a :class:`local` object, including their
+:attr:`~object.__dict__` attribute, are for whatever thread was current
+at the time the attribute was read. For that reason, you generally
+don't want to save these values across threads, as they apply only to
+the thread they came from.
+
+You can create custom :class:`local` objects by subclassing the
+:class:`local` class::
+
+ >>> class MyLocal(local):
+ ... number = 2
+ ... def __init__(self, /, **kw):
+ ... self.__dict__.update(kw)
+ ... def squared(self):
+ ... return self.number ** 2
-Thread-local data is data whose values are thread specific. To manage
-thread-local data, just create an instance of :class:`local` (or a
-subclass) and store attributes on it::
+This can be useful to support default values, methods and
+initialization. Note that if you define an :py:meth:`~object.__init__`
+method, it will be called each time the :class:`local` object is used
+in a separate thread. This is necessary to initialize each thread's
+dictionary.
- mydata = threading.local()
- mydata.x = 1
+Now if we create a :class:`local` object::
-The instance's values will be different for separate threads.
+ >>> mydata = MyLocal(color='red')
+
+we have a default number::
+
+ >>> mydata.number
+ 2
+
+an initial color::
+
+ >>> mydata.color
+ 'red'
+ >>> del mydata.color
+
+And a method that operates on the data::
+
+ >>> mydata.squared()
+ 4
+
+As before, we can access the data in a separate thread::
+
+ >>> log = []
+ >>> thread = threading.Thread(target=f)
+ >>> thread.start()
+ >>> thread.join()
+ >>> log
+ [[('color', 'red')], 11]
+
+without affecting this thread's data::
+
+ >>> mydata.number
+ 2
+ >>> mydata.color
+ Traceback (most recent call last):
+ ...
+ AttributeError: 'MyLocal' object has no attribute 'color'
+
+Note that subclasses can define :term:`__slots__`, but they are not
+thread local. They are shared across threads::
+
+ >>> class MyLocal(local):
+ ... __slots__ = 'number'
+
+ >>> mydata = MyLocal()
+ >>> mydata.number = 42
+ >>> mydata.color = 'red'
+
+So, the separate thread::
+
+ >>> thread = threading.Thread(target=f)
+ >>> thread.start()
+ >>> thread.join()
+
+affects what we see::
+
+ >>> mydata.number
+ 11
.. class:: local()
A class that represents thread-local data.
- For more details and extensive examples, see the documentation string of the
- :mod:`!_threading_local` module: :source:`Lib/_threading_local.py`.
-
.. _thread-objects:
-Thread Objects
---------------
+Thread objects
+^^^^^^^^^^^^^^
The :class:`Thread` class represents an activity that is run in a separate
thread of control. There are two ways to specify the activity: by passing a
@@ -448,11 +621,11 @@ since it is impossible to detect the termination of alien threads.
an error to :meth:`~Thread.join` a thread before it has been started
and attempts to do so raise the same exception.
- If an attempt is made to join a running daemonic thread in in late stages
+ If an attempt is made to join a running daemonic thread in late stages
of :term:`Python finalization <interpreter shutdown>` :meth:`!join`
raises a :exc:`PythonFinalizationError`.
- .. versionchanged:: next
+ .. versionchanged:: 3.14
May raise :exc:`PythonFinalizationError`.
@@ -536,8 +709,8 @@ since it is impossible to detect the termination of alien threads.
.. _lock-objects:
-Lock Objects
-------------
+Lock objects
+^^^^^^^^^^^^
A primitive lock is a synchronization primitive that is not owned by a
particular thread when locked. In Python, it is currently the lowest level
@@ -629,8 +802,8 @@ All methods are executed atomically.
.. _rlock-objects:
-RLock Objects
--------------
+RLock objects
+^^^^^^^^^^^^^
A reentrant lock is a synchronization primitive that may be acquired multiple
times by the same thread. Internally, it uses the concepts of "owning thread"
@@ -739,8 +912,8 @@ call release as many times the lock has been acquired can lead to deadlock.
.. _condition-objects:
-Condition Objects
------------------
+Condition objects
+^^^^^^^^^^^^^^^^^
A condition variable is always associated with some kind of lock; this can be
passed in or one will be created by default. Passing one in is useful when
@@ -917,8 +1090,8 @@ item to the buffer only needs to wake up one consumer thread.
.. _semaphore-objects:
-Semaphore Objects
------------------
+Semaphore objects
+^^^^^^^^^^^^^^^^^
This is one of the oldest synchronization primitives in the history of computer
science, invented by the early Dutch computer scientist Edsger W. Dijkstra (he
@@ -998,7 +1171,7 @@ Semaphores also support the :ref:`context management protocol <with-locks>`.
.. _semaphore-examples:
-:class:`Semaphore` Example
+:class:`Semaphore` example
^^^^^^^^^^^^^^^^^^^^^^^^^^
Semaphores are often used to guard resources with limited capacity, for example,
@@ -1026,8 +1199,8 @@ causes the semaphore to be released more than it's acquired will go undetected.
.. _event-objects:
-Event Objects
--------------
+Event objects
+^^^^^^^^^^^^^
This is one of the simplest mechanisms for communication between threads: one
thread signals an event and other threads wait for it.
@@ -1083,8 +1256,8 @@ method. The :meth:`~Event.wait` method blocks until the flag is true.
.. _timer-objects:
-Timer Objects
--------------
+Timer objects
+^^^^^^^^^^^^^
This class represents an action that should be run only after a certain amount
of time has passed --- a timer. :class:`Timer` is a subclass of :class:`Thread`
@@ -1121,8 +1294,8 @@ For example::
only work if the timer is still in its waiting stage.
-Barrier Objects
----------------
+Barrier objects
+^^^^^^^^^^^^^^^
.. versionadded:: 3.2