Trying to use threading.local()
I'm originally posted this on the Python-Ideas list, but this is probably
from threading import Thread, local
func.__params__ = local()
func.__params__.value = value
print("called from thread %s" % i)
assert func.__params__.value == i
value = func.__params__.value
if value != i:
print("mismatch", i, value)
for i in range(5):
t = Thread(target=worker, args=(i,))
When I run that, each of the threads print their "called from ..."
message, the assertions all pass, then a couple of seconds later they
consistently all raise exceptions:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/threading.py", line 914, in
File "/usr/local/lib/python3.5/threading.py", line 862, in run
File "<stdin>", line 5, in worker
AttributeError: '_thread._local' object has no attribute 'value'
What am I doing wrong?
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson