スレッドの場合は、thread
(またはPython 3の_thread
)モジュールを使用して、例外を指定してスレッドを終了させることができます。thread.exit()
を呼び出します。 documentationから
:
- thread.exit():SystemExit例外を発生させます。キャッチされていない場合、 これはスレッドをサイレントモードで終了させます。 (あなたの処理が設定されている方法に応じて)
クリーナー方法は、それまで待機するメインスレッドからjoin()
メソッドを呼び出し、インスタンス変数を使用して処理し、出口を停止するスレッドを通知するであろうスレッドは終了します。
例:
class MyThread(threading.Thread):
def __init__(self):
super(MyThread, self).__init__()
self._stop_req = False
def run(self):
while not self._stop_req:
pass
# processing
# clean up before exiting
def stop(self):
# triggers the threading event
self._stop_req = True;
def main():
# set up the processing thread
processing_thread = MyThread()
processing_thread.start()
# do other things
# stop the thread and wait for it to exit
processing_thread.stop()
processing_thread.join()
if __name__ == "__main__":
main()
あなたはpythonのプロセスやPythonのスレッドを停止しようとしていますか? –
それは別のスレッドです。 –