Suppose I have this:
from threading import local
tl = local()
At various points, different threads in a thread pool will set something on the thread local:
tl.resource = <something>
Is it possible to iterate over all tl.resource set by each thread?
Not in Python, although you might be able to dig around in the interpreter state in C and acquire this information (although I doubt it). It would be reasonable to assume that this might be possible if
local()returned a singleton for each thread, but it does not – callinglocal()multiple times in the same thread will generate multiple unique thread-local storage instances (not return a reference to the same object) and as such there is not a single field in the Python internal thread structure that holds this data, making it difficult to inspect.