threading.Thread()
The threading module is a standard library for running tasks concurrently using threads. Multiple threads run simultaneously, allowing other work to proceed while waiting for I/O (file reads/writes, network communication, etc.). Due to Python's GIL (Global Interpreter Lock), CPU-intensive computations do not run in true parallel. Use multiprocessing instead for CPU-bound tasks.
Syntax
import threading
# Create and start a thread
t = threading.Thread(target=function, args=(arg,))
t.start() # Start the thread
t.join() # Wait for the thread to finish
# Mutual exclusion (Lock)
lock = threading.Lock()
with lock:
# Critical section
pass
Classes and Methods
| Class / Method | Description |
|---|---|
| Thread(target, args, kwargs) | Creates a thread object. |
| t.start() | Starts the thread. |
| t.join(timeout=None) | Blocks the main thread until the thread finishes. |
| t.is_alive() | Returns whether the thread is still running. |
| t.daemon = True | Marks the thread as a daemon thread. It exits automatically when the main thread ends. |
| threading.Lock() | Creates a lock object for mutual exclusion. |
| lock.acquire() | Acquires the lock (prefer using with instead). |
| lock.release() | Releases the lock (prefer using with instead). |
| threading.current_thread() | Returns the currently running thread object. |
Sample Code
import threading
import time
# Basic thread creation and execution
def download(url, name):
print(f"[{name}] Download started: {url}")
time.sleep(2) # Simulate network processing
print(f"[{name}] Download complete!")
# Sequential execution (takes about 6 seconds)
# for url in urls: download(url)
# Concurrent execution (takes about 2 seconds)
urls = ['https://example.com/file1', 'https://example.com/file2', 'https://example.com/file3']
threads = []
for i, url in enumerate(urls):
t = threading.Thread(target=download, args=(url, f"Thread-{i+1}"))
threads.append(t)
t.start()
for t in threads:
t.join() # Wait for all threads to finish
print("All downloads complete")
# Lock: mutual exclusion for shared resources
counter = 0
lock = threading.Lock()
def increment(n):
global counter
for _ in range(n):
with lock: # Acquire the lock before updating the counter
counter += 1
threads = [threading.Thread(target=increment, args=(10000,)) for _ in range(5)]
for t in threads: t.start()
for t in threads: t.join()
print(f"Counter: {counter}") # 50000 (without Lock, a race condition would give wrong results)
# Thread using class inheritance
class WorkerThread(threading.Thread):
def __init__(self, task_id):
super().__init__(daemon=True)
self.task_id = task_id
self.result = None
def run(self):
# run() executes in a separate thread when start() is called
time.sleep(1)
self.result = self.task_id ** 2
workers = [WorkerThread(i) for i in range(5)]
for w in workers: w.start()
for w in workers: w.join()
print([w.result for w in workers]) # [0, 1, 4, 9, 16]
Notes
When threads access shared resources (global variables, files, etc.) without mutual exclusion (Lock), race conditions can occur. If multiple threads read and write at the same time, the results can be unpredictable.
Python's GIL (Global Interpreter Lock) ensures thread-safe access to Python objects, but it also means only one thread can execute Python code at a time. As a result, threading is not effective for CPU-bound tasks such as numerical computation or image processing, but it is useful for I/O-bound tasks such as file I/O and HTTP requests. Use multiprocessing to parallelize CPU-bound tasks.
If you find any errors or copyright issues, please contact us.