I'm trying to store some data from a JSON file into a class variable that can be accessed by any other functions outside the class. The data in the JSON file will always be different, so I'm using a thread.
This is an example of my code.
class fruitStand(threading.Thread):
fruitAmount = []
def __init__(self):
threading.Thread.__init__(self)
def run(self):
with open("fruitDatafile.json", "r") as fruitDatafile:
fruitData = json.load(fruitDatafile)
fruitValue = [
fruitData["apples"],
fruitData["pears"],
fruitData["watermelons"],
fruitData["lemons"]]
while True:
self.fruitAmount.clear()
for item in fruitValue:
self.fruitAmount.append(item)
time.sleep(3100)
fStand = fruitStand()
fStand.start()
print(fStand.fruitAmount)
The data seems to be stored correctly into the fruitAmount
variable, but when I try to access it from outside the class, it shows as if there's nothing in it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…