I have multiple python files in different folders that work together to make my program function. They consist of a main.py
file that creates new threads for each file and then starts them with the necessary parameters. This works great while the parameters are static, but if a variable changes in the main.py
it doesn't get changed in the other files. I also can't import the main.py
file into otherfile.py
to get the new variable since it is in a previous dir.
I have created an example below. What should happen is that the main.py
file creates a new thread and calls otherfile.py
with set params. After 5 seconds, the variable in main.py
changes and so should the var in otherfile (so it starts printing the number 5 instead of 10), but I haven't found a solution to update them in otherfile.py
The folder structure is as follows:
|-main.py
|-other
|
otherfile.py
Here is the code in both files:
main.py
from time import sleep
from threading import Thread
var = 10
def newthread():
from other.otherfile import loop
nt = Thread(target=loop(var))
nt.daemon = True
nt.start()
newthread()
sleep(5)
var = 5 #change the var, otherfile.py should start printing it now (doesnt)
otherfile.py
from time import sleep
def loop(var):
while True:
sleep(1)
print(var)