私はPythonスレッドを作成します.1つはstart()メソッドを呼び出すことで実行するキックです.1つはスレッド内のフラグを監視します.そのflag == Trueの場合、ユーザーがスレッドを実行し続けることを望んでいないことがわかっているので、嘘をつきますハウスクリーニングを行い、スレッドを終了します。
ただし、スレッドを終了できませんでした。thread.join() 、 thread.exit() 、thread.quit() を試しましたが、すべて例外がスローされました。
私のスレッドは次のようになります。
EDIT 1 : core() 関数は標準の run() function 内で呼び出されることに注意してください。ここでは示していません。
EDIT 2 : StopFlag が true のときに sys.exit() を試してみたところ、スレッドが終了したように見えます! 一緒に行っても安全ですか?
class workingThread(Thread):
def __init__(self, gui, testCase):
Thread.__init__(self)
self.myName = Thread.getName(self)
self.start() # start the thread
def core(self,arg,f) : # Where I check the flag and run the actual code
# STOP
if (self.StopFlag == True):
if self.isAlive():
self.doHouseCleaning()
# none of following works all throw exceptions
self.exit()
self.join()
self._Thread__stop()
self._Thread_delete()
self.quit()
# Check if it's terminated or not
if not(self.isAlive()):
print self.myName + " terminated "
# PAUSE
elif (self.StopFlag == False) and not(self.isSet()):
print self.myName + " paused"
while not(self.isSet()):
pass
# RUN
elif (self.StopFlag == False) and self.isSet():
r = f(arg)