I want change global variable in module1.py by module2.py.
module1.py
#!/usr/bin/env python
import threading
import module2 as module22
import time
values=True
def main():
print "m"
def thread():
while(values):
print "moduel1"
time.sleep(0.50)
print "END PROGRAM"
def change():
print "change"
values=False
if __name__ == "__main__":
t2=threading.Thread(target=module22.main())
t1=threading.Thread(target=thread())
t1.start()
t2.start()
t1.join()
t2.join()
module2.py
#!/usr/bin/env python
import module1 as module11
def main():
print "module2"
module11.change()
if __name__ == "__main__":
main()
When I run sudo python module1.py:
Result is here
module2
change
moduel1
moduel1..
I want get result
module2
change
END PROGRAM
解决方案
You need to declare values as global, if you dont do this, the variable is handled as new local variable.
def change():
global values
print "change"
values=False