Glossary

Thread

A thread is the smallest unit of execution scheduled by the operating system. Multiple threads within a single process share the same memory space, making it easy to share data between them — but also creating risks of data corruption if access is not synchronised. Python's threading module provides the interface for creating and managing threads.

In CPython, threads are limited by the Global Interpreter Lock (GIL), which allows only one thread to execute Python bytecode at a time. This means threads do not provide true parallelism for CPU-bound tasks. However, threads are highly effective for I/O-bound tasks — network requests, file operations, database queries — because the GIL is released during I/O operations, allowing other threads to run.

The threading module provides Thread objects (create with a target function or by subclassing), Lock and RLock for mutual exclusion, Event for signalling between threads, Semaphore for limiting concurrent access, and Condition for complex synchronisation patterns. For most use cases, concurrent.futures.ThreadPoolExecutor provides a simpler, higher-level interface that manages a pool of worker threads and returns Future objects.

Related terms: Process, GIL, Concurrent Futures

Discussed in:

This site is currently in Beta. Please email Chris Paton (cpaton@gmail.com) with any suggestions, questions or comments.