On this page
  1. threading.local
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

threading.local

Prev: threading.gettrace | Up: threading | Next: threading.Lock
Rel: threading.active_count, threading.Barrier, threading.BoundedSemaphore, threading.BrokenBarrierError
Ref: docs.python.org — threading.local
Tags: #public #python3 #stdlib #threading


Import

import threading
from threading import local

Signature & Overview

threading.local()

Thread-local data

Usage

import threading

tls = threading.local()
tls.user = "main"
seen: list[str] = []

def worker() -> None:
    tls.user = "worker"
    seen.append(tls.user)

t = threading.Thread(target=worker)
t.start()
t.join()
assert tls.user == "main" and seen == ["worker"]

Gotchas

REPL Inspect

from threading import local
help(local)

Linked from

Related notes