Thread
Python provides threading and multiprocessing modules for concurrent execution. Threads share memory and are ideal for I/O-bound tasks, while processes offer true parallelism for CPU-bound work. The guide walks through Thread Basics, Locks & Synchronization, Thread Pool Executor, Process Pool Executor, Working with Futures. Create a thread with `Thread(target=func, args=(arg,))`, then `.start()` to launch and `.join()` to wait for completion. Daemon threads (set `daemon=True`) exit when the main thread exits. Threads share the same global interpreter lock (GIL). Use `threading.Lock` to protect shared resources. Call `.acquire()` before accessing and `.release()` after. Better: use the lock as a context manager `with lock:`. `RLock` allows re-entrant locking from the same thread. `concurrent.futures.ThreadPoolExecutor(max_workers=N)` manages a pool of threads. `.submit(fn)` returns a `Future`. `.map(fn, iterable)` applies the function across threads. Great for parallel I/O operations like downloading multiple URLs. The guide is organized into 5 sections that build on each other, each pairing a prose explanation with runnable code.