ThreadLocalStorage#

class barecat.util.threading.ThreadLocalStorage(threadsafe)[source]#

Storage that lazily creates values and handles closing.

Encapsulates the common pattern of: - Lazily creating a value on first access using a factory - Closing the value when done - Optionally being thread-local (each thread gets its own value)

Usage:

storage = ThreadLocalStorage(threadsafe=True)

# Get or create value sock = storage.get(make_socket)

# Close and clear storage.close()

Parameters:

threadsafe (bool)

Instance Methods#

get(factory)

Get the stored value, creating it with factory() if not present.

close()

Close and clear the stored value (current thread only if threadsafe).