Why does this code work:
data = local()
def main():
proc = multiprocessing.Process(target = foo)
proc.start()
def foo():
data.stuff = 'happiness'
bar()
def bar():
print data.stuff
But this code does not work?
def main():
proc = multiprocessing.Process(target = foo)
proc.start()
def foo():
local().stuff = 'happiness'
bar()
def bar():
print local().stuff
We have code divided to several modules and it has multiple threads. We need to pass values to the threads that need to presist in all those modules.
For instance:
We instantiate a parcel of work.
# inside module_name_a.py
work = Work(a, b, c)
proc = multiprocessing.Process(target = work.do_this)
proc.start()
We are doing this work and we need to store some data globally in the thread. We can't change the API of Detail.
# inside work_module_b.py
class Work():
.
.
.
def do_this():
detail = Detail(x, y, z)
local().stuff = 'important'
detail.handle_this()
We need the body of handle_this()
to "see" the values inside threading.local()
# inside detail_module_b.py
class Detail():
.
.
.
def do_this():
detail = Detail(x, y, z)
local().stuff = 'important'
detail.handle_this()
Any advice about proper use of threading.local() ?