【发布时间】:2020-04-25 19:04:17
【问题描述】:
我有 2 个 python 模块。其中一个有一个布尔变量,该变量不断变化(根据输入)。
在第二个模块中,我根据第一个模块中布尔变量的值做一些事情。
我希望这个共享变量同时更新,但自从我第一次导入它后,它的值并没有改变。
我尝试使用全局变量,但这也不起作用!
简而言之:
module1.py:
my_boolean = True
while True:
a = input()
if a == 'change':
my_boolean = not my_boolean
在module2.py中:
import module1
while True:
print (module1.my_boolean)
#The output of print is constant and is not real-time.
共享此变量及其更新值的正确方法是什么?
【问题讨论】:
-
你有两个 while True 循环你也有两个线程吗?因为否则你的代码将无法工作......
-
有两个不同的模块!两种不同的过程。为什么我需要线程??!!
-
您将 module1 导入到 module2 中,因此您有一个进程。
标签: python