class1.stop_callback(ねじクラスを介して変数を渡す従ってclass3.stopあるべき=真
をclass2.stop従ってclass1.stop =真
を設定します本当ですが、そうではありません。
class1.stop_callback()はプログラムを停止する必要がありますが、それは行いません。 私は何が間違っていますか?
あなたはPythonでrepl.it https://repl.it/@bahtsiz_bedevi/classtest
import threading
import time
class Class1(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.stop = False
def stop_callback(self):
self.stop = True
def run(self):
class2 = Class2()
class2.stop = self.stop
class2.start()
while True:
time.sleep(1)
print("{} stop status: {}".format(self.__class__, "True" if self.stop else "False"))
if self.stop:
break
class Class2(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.stop = False
def run(self):
class3 = Class3()
class3.stop = self.stop
while True:
time.sleep(1)
print("{} stop status: {}".format(self.__class__, "True" if self.stop else "False"))
if self.stop:
break
class3.foo()
class Class3:
def __init__(self):
self.stop = False
def foo(self):
while True:
time.sleep(1)
print("{} stop status: {}".format(self.__class__, "True" if self.stop else "False"))
if self.stop:
break
class1 = Class1()
class1.start()
for i in range(10):
time.sleep(1)
class1.stop_callback()
あなたはすでに 'class1.stop'の値を' class2.stop'にコピーした後に 'class1.stop'を' True'に設定してください。私はここで何か見落としていますか?あるいは 'class2.stop'を' class1.stop'への参照の一種として使用したいのですか? –
希望する出力は何ですか、プログラムで何をしたいですか? – PrestonM
@PrestonM "したがってclass3.stopはTrueであるべきですが、そうではありません。それは問題ですが、出力は重要ではありません。 –