Distinguish Thread and Interpreter State
Core idea
CPython distinguishes a process, interpreter state and thread state. An interpreter owns Python execution resources such as its module registry; a thread state associates execution with an interpreter. Ordinary threading.Thread workers share one interpreter’s objects. Subinterpreters have separate interpreter state, but are not OS process isolation or security sandboxes. In CPython 3.13, subinterpreter management is available through the C API; this lesson does not depend on private Python modules.
import sys, threading, queueresult = queue.Queue()registry = sys.modulesmain = threading.current_thread()def worker(): result.put((sys.modules is registry, threading.current_thread() is main))t = threading.Thread(target=worker)t.start()t.join()print(result.get_nowait())The worker uses a different thread object but sees the same module registry in ordinary threading.
Expected output:
(True, False)Separate thread-local bindings
import threading, queuestate = threading.local()state.label = 'main'result = queue.Queue()def worker(): result.put(hasattr(state, 'label')) state.label = 'worker' result.put(state.label)t = threading.Thread(target=worker)t.start()t.join()print(result.get_nowait(), result.get_nowait(), state.label)Thread-local storage differs per thread even though both threads share the same interpreter.
Expected output:
False worker mainCommon mistake
Do not assume each thread has its own interpreter or that the GIL protects every multi-step invariant. Interpreter isolation and GIL ownership are distinct configuration and version questions.
Practice
Draw a process containing two interpreters and multiple thread states. Contrast ordinary threads sharing a module with separate interpreter registries; inspect the C API docs rather than invoking private APIs.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Interpreter state
Check your understanding of interpreter state.
What is printed?
import threadingprint(threading.current_thread() is threading.main_thread())What is printed?
import threadingx=threading.local(); x.n=3print(x.n)Do ordinary threading.Thread workers automatically create separate interpreters?
Are subinterpreters a security sandbox?