If you are using a version of pure-python threading.local ( from _threading_local import local ), this is possible:
for t in threading.enumerate(): for item in t.__dict__: if isinstance(item, tuple): # Each thread `local` state is kept in a tuple stored in its __dict__ print("Thread local is %s" % t.__dict__[item])
Here is an example of this in action:
from _threading_local import local import threading import time l = local() def f(): global l l.ok = "HMM" time.sleep(50) if __name__ == "__main__": l.ok = 'hi' t = threading.Thread(target=f) t.start() for t in threading.enumerate(): for item in t.__dict__: if isinstance(item, tuple): print("Thread local is %s" % t.__dict__[item])
Output:
Thread local is {'ok': 'hi'} Thread local is {'ok': 'HMM'}
This takes advantage of the fact that the pure-python local implementation stores each state of the local thread in the Thread __dict__ object, using the tuple object as a key:
>>> threading.current_thread().__dict__ { ..., ('_local__key', 'thread.local.140466266257288'): {'ok': 'hi'}, ...}
If you use a local implementation written in C (usually this is the case if you just use from threading import local ), I'm not sure how to do it / if you can do it.
dano
source share