Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
384 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...