0
私はイベントオブジェクトを待つように言った後、なぜ以下のコードのwhileループがまだ動いているのか不思議です。待機中にPythonスレッドがまだ実行されているのはなぜですか?
import threading, time
msg = threading.Event()
def sec():
print "second thread starting now.."
counter = 0
while True:
#while msg.isSet():
print "hello"
print counter
counter += 1
time.sleep(3)
msg.wait()
secThread = threading.Thread(target = sec)
secThread.start()
print "a"
msg.set()
time.sleep(5)
print "b"
msg.set()
そして、私は次のように出力
second thread starting now..a
hello
0
hello
1
b
hello
2
hello
3
....
(keeps going)
を取得し、私はループしながら、コメントアウトして、それを修正することができますが、私はそれがまだ走った理由として興味があります。メインスレッドは実行されているので、2番目のスレッドを実行できるようにスレッドイベントを設定する必要はありません。
ありがとうございます。
ありがとうございます。私は、 "待つ"が旗を元に戻すと思っている間違いをしましたが、そうではありません。フラグをリセットするには、あなたが言ったように、代わりにクリアを使うべきです。 – Arrow