[英]Accessing a variable from another file that's in a loop in Python
I have a file that is reading measurements in the main. 我有一个主要读取测量值的文件。 This function is in a while True loop. 该函数处于True循环中。 Inside this loop I want to change a variable as it goes through the process. 在此循环中,我想在过程中更改变量。 No problems with setting that up. 设置没有问题。 The issue I have is accessing this variable from another file. 我遇到的问题是从另一个文件访问此变量。
File 1: 文件1:
def main()
print("obtaining token")
obtainnewtoken()
while True:
print("******LOOP****** + str(i)")
(read measurement stuff )
postTrue = True
return postTrue
File 2: 档案2:
from File1 import *
newPostTrue = main()
def codechecker():
print(newPostTrue)
When I run both files simultaneously File2 just runs the main of File1. 当我同时运行两个文件时,File2仅运行File1的主文件。 How Do I access a variable that is in the loop in another file? 如何访问另一个文件中循环的变量?
Also I would still like to run both files separately. 另外,我仍然想分别运行两个文件。 This setup is temporary. 此设置是临时的。
You can use something called as generators which will "Yield" a value once, and then you can use the next() function to get the next value from the generator. 您可以使用一种称为“生成器”的东西,它将一次“屈服”一个值,然后可以使用next()函数从生成器中获取下一个值。
File_1: File_1:
def Generator():
i = 0
while True:
print("******LOOP******" + str(i))
i += 1
yield i
File_2: File_2:
from File_1 import *
newPostTrue = Generator()
def codechecker():
j = next(newPostTrue)
while (j < 10):
print(j)
j = next(newPostTrue)
codechecker()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.