부모 스레드가 아직 살아 있는지/붙어 있는지 확인하는 방법을 궁금합니다. 기본적으로 자식에게 명령을 보내는 부모 스레드가 있습니다. 부모 스레드가 죽거나 교착 상태에 도달하면 자식이 계속 살기를 원하지 않습니다. 아래는 지금까지 구현 한 기본 프레임 워크입니다.부모 스레드가 실행 중인지 확인하십시오.
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()
내가 가진 한 가지 아이디어는 부모로부터 아이에게 컴퓨터 시간을 주기적으로 보내는 것입니다. 시간이 설정된 값보다 크면 어린이가 사망합니다. 더 쉽거나 더 효과적인 방법이 있습니까? 또한 파이썬 설명서 내에서 스레딩 클래스 내에 isAlive 메서드가 있지만 사용 방법을 잘 모르고 있습니다.