Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-124210: Add introduction to threading docs #127046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
May 16, 2025
Merged
Changes from 15 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
58 changes: 58 additions & 0 deletions 58 Doc/library/threading.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,64 @@ level :mod:`_thread` module.

.. include:: ../includes/wasm-notavail.rst

Introduction
------------
merwok marked this conversation as resolved.
Show resolved Hide resolved

The :mod:`threading` module provides a way to run multiple threads (smaller
donBarbos marked this conversation as resolved.
Show resolved Hide resolved
units of a process) concurrently within a single process. It allows for the
donBarbos marked this conversation as resolved.
Show resolved Hide resolved
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,
donBarbos marked this conversation as resolved.
Show resolved Hide resolved
where much of the time is spent waiting for external resources.

A typical use case for :mod:`threading` includes managing a pool of worker
donBarbos marked this conversation as resolved.
Show resolved Hide resolved
threads that can process multiple tasks concurrently. This basic example of
donBarbos marked this conversation as resolved.
Show resolved Hide resolved
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()
donBarbos marked this conversation as resolved.
Show resolved Hide resolved

# Wait for all threads to finish
for t in threads:
t.join()

GIL and Performance Considerations
donBarbos marked this conversation as resolved.
Show resolved Hide resolved
----------------------------------

Unlike the :mod:`multiprocessing` module, which uses separate processes to
bypass the :term:`Global Interpreter Lock <global interpreter lock>`, the
donBarbos marked this conversation as resolved.
Show resolved Hide resolved
threading module operates within a single process, meaning that all threads
share the same memory space. However, the :term:`GIL` limits the performance
donBarbos marked this conversation as resolved.
Show resolved Hide resolved
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, experimental :term:`free-threaded <free threading>` builds
can disable the :term:`GIL`, enabling true parallel execution of threads, but
donBarbos marked this conversation as resolved.
Show resolved Hide resolved
this feature is not available by default. (See :pep:`703`.)
donBarbos marked this conversation as resolved.
Show resolved Hide resolved

This module defines the following functions:
donBarbos marked this conversation as resolved.
Show resolved Hide resolved


Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.