この問題はほぼ解決しましたが、正しい方向に微調整する必要があると思います。
一定の時間が経過するか、ユーザーが中断するまで、5 秒ごとに何かを実行したいと考えています(この場合、終了する前にループの反復を終了します)。
import time
import threading
def do_something():
T0 = time.clock()
while (time.clock() - T0) < 60 and not e.isSet(): #as long as 60s haven't elapsed
#and the flag is not set
#here do a bunch of stuff
time.sleep(5)
thread = threading.Thread(target=do_something, args=())
thread.start()
e = threading.Event()
while thread.isAlive():
#here I want the main thread to wait for a keypress and, if it receives it,
#set the event e, which will cause the thread to finish its work.
その最後の行を機能させる方法がわかりません。ループ内で使用raw_input()
すると、スレッドが作業を終了するかどうかにかかわらず、ユーザーが Enter キーを押すまでブロックされます。私がやりたいことをする別のモジュールはありますか?
編集: Windows XP を使用しています。