Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/alex-ber/AlexBerUtils/llms.txt

Use this file to discover all available pages before exploring further.

Thread-local storage keeps a separate copy of a variable for every thread. AlexBerUtils provides three helpers that sit on top of Python’s threading.local() to make this pattern explicit and safe.

Functions

threadlocal_var

Retrieves a thread-local variable by name, creating it with the given factory if it does not yet exist for the current thread.
from alexber.utils.thread_locals import threadlocal_var
from threading import local

_locals = local()

# Returns the existing value or calls list() to create it
buffer = threadlocal_var(_locals, 'buffer', list)
buffer.append('item')
thread_locals
threading.local
required
The threading.local instance that holds per-thread state.
varname
string
required
The attribute name to look up (or create) on thread_locals.
factory
Callable
required
A zero-argument callable used to produce the initial value when the variable does not exist for the current thread. Any extra positional or keyword arguments passed after factory are forwarded to it.

get_threadlocal_var

Retrieves a thread-local variable that must already be initialised. Raises ValueError if the variable is None or was never set.
from alexber.utils.thread_locals import get_threadlocal_var
from threading import local

_locals = local()

# Somewhere during thread startup:
_locals.connection = create_db_connection()

# Later — will raise ValueError if 'connection' was never set:
conn = get_threadlocal_var(_locals, 'connection')
thread_locals
threading.local
required
The threading.local instance.
varname
string
required
The attribute name to retrieve. A ValueError is raised if the value is None.
Use get_threadlocal_var only after the variable has been initialised for the current thread. If you are unsure whether it has been set yet, use threadlocal_var with an appropriate factory instead.

del_threadlocal_var

Deletes a thread-local variable silently. Unlike delattr, it does not raise if the variable does not exist.
from alexber.utils.thread_locals import del_threadlocal_var
from threading import local

_locals = local()

# Safe to call even if 'connection' was never set
del_threadlocal_var(_locals, 'connection')
thread_locals
threading.local
required
The threading.local instance.
varname
string
required
The attribute name to delete. No error is raised if the attribute does not exist.

Common pattern

The three functions are typically used together to manage the full lifecycle of a per-thread resource:
from threading import local, Thread
from alexber.utils.thread_locals import (
    threadlocal_var,
    get_threadlocal_var,
    del_threadlocal_var,
)

_locals = local()

def process():
    # Initialise once per thread
    conn = threadlocal_var(_locals, 'db', create_db_connection)

    # Use the connection (guaranteed to exist)
    conn = get_threadlocal_var(_locals, 'db')
    conn.execute('SELECT 1')

    # Clean up at end of thread
    del_threadlocal_var(_locals, 'db')

threads = [Thread(target=process) for _ in range(4)]
for t in threads:
    t.start()
for t in threads:
    t.join()
Each thread gets its own independent copy of db. Changes in one thread never affect another.

Build docs developers (and LLMs) love