Skip to content
Merged
Changes from 4 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
52 changes: 50 additions & 2 deletions Doc/library/threading.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

**Source code:** :source:`Lib/threading.py`

--------------

Comment thread
merwok marked this conversation as resolved.
This module constructs higher-level threading interfaces on top of the lower
level :mod:`_thread` module.

Expand Down Expand Up @@ -47,6 +45,56 @@ level :mod:`_thread` module.

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

--------------

Introduction
------------
Comment thread
merwok marked this conversation as resolved.

The :mod:`threading` module provides a way to run multiple threads (smaller
Comment thread
donbarbos marked this conversation as resolved.
Outdated
units of a process) concurrently within a single process. It allows for the
Comment thread
donbarbos marked this conversation as resolved.
Outdated
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 reading from or writing to files, or making network requests,
Comment thread
donbarbos marked this conversation as resolved.
Outdated
Comment thread
donbarbos marked this conversation as resolved.
Outdated
where much of the time is spent waiting for external resources.

Unlike the :mod:`multiprocessing` module, which uses separate processes to
bypass the :term:`Global Interpreter Lock <global interpreter lock>`, the
Comment thread
donbarbos marked this conversation as resolved.
Outdated
threading module operates within a single process, meaning that all threads
share the same memory space. However, the :term:`GIL` limits the performance gains of
threading when it comes to CPU-bound tasks, as only one thread can execute
Comment thread
donbarbos marked this conversation as resolved.
Outdated
Python bytecode at a time. Despite this, threads remain a useful tool for
achieving concurrency in many scenarios.
Comment thread
donbarbos marked this conversation as resolved.
Outdated

A typical use case for :mod:`threading` includes managing a pool of worker
threads that can process multiple tasks concurrently. This basic example of
Comment thread
donbarbos marked this conversation as resolved.
Outdated
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)
Comment thread
donbarbos marked this conversation as resolved.
Outdated
print(f"crawl ended for {link}")

links = [
"https://example.com",
"https://another-example.com",
"https://yet-another-example.com"
Comment thread
donbarbos marked this conversation as resolved.
Outdated
]

# 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)
t.start()
Comment thread
donbarbos marked this conversation as resolved.
Outdated

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

This module defines the following functions:
Comment thread
donbarbos marked this conversation as resolved.


Expand Down