I have two files
runner.py- main
import secondthread
my_dict = {}
def action1():
my_dict["i"] = 2
if __name__ == "__main__":
action1()
secondthread.action2()
secondthread.py
import runner
def action2():
try:
print("Thread2: dict value is = {}".format(runner.my_dict["i"]))
except Exception as e:
print("Thread2: dict haven't got updated value, len of dict is: {}".format(len(runner.my_dict)))
I am unsure why despite sequentially running action1 then action2 does not update the dictionary with the new key-value pair.
I have tried setting my_dict as a global variable in secondthread.py and it does not work. I understand that perhaps the circular dependency (runner.py depends on secondthread.py and secondthread.py depends on runner.py may be problematic, but I am not sure how else to read my_dict from secondthread.py).
Update1:
I have also tried inside secondthread.py which still results in the same error.
from runner import my_dict
Wouldn't this method already retrieve my_dict from global namespace?