親スレッドがまだ生きているかスタックしているかどうかを確認する方法を知りたいです。基本的に、子にコマンドを送信する親スレッドがあります。親スレッドが停止したり、デッドロック状態になったりした場合、子スレッドを存続させたくありません。以下は、これまでの私の実装の基本的なフレームワークです。
from Queue import Queue
from threading import Thread
class myClass:
def __init__(self):
self.currentCommand = Queue()
t = Thread(target=self._run)
t.start()
def close(self):
self._sendCommand("close")
def _run(self):
while True:
if self.currentCommand.empty():
pass
#do some task
else:
command = self.currentCommand.get()
if command == "close":
#clean up
self.currentCommand.task_done()
break
else:
#do command task
self.currentCommand.task_done()
def _sendCommand(self, command):
self.currentCommand.put(command)
self.currentCommand.join()
私が持っているアイデアの 1 つは、コンピュータの時間を親から子に定期的に送信することです。時間が設定値よりも大きい場合、子供は死亡します。もっと簡単で効果的な方法はありますか?また、Python のドキュメントには、スレッド クラス内に isAlive メソッドがありますが、その使用方法がわかりません。