다른 스레드에서 실행중인 worker
객체에서 stopThread
을 호출하여 실행중인 스레드를 Python에서 부드럽게 종료하려고합니다.스레드에서 실행중인 객체에서 함수를 호출 할 수 없습니다.
그러나 그렇게 나에게 오류주고있다 :
AttributeError: 'Thread' object has no attribute 'stopThread'
우리가 어떻게이 문제를 해결할 수
를?AttributeError: 'Thread' object has no attribute 'stopThread'
^^^^^^^^^^^^^^
가 의도로 Worker
객체
t = threading.Thread(target=startWorker)
때문에 .. 그리고하지 :
import threading
import time
class Worker(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.stopRequest = threading.Event()
def doSomething(self):
while True:
if not self.stopRequest.isSet():
print 'Doing something'
time.sleep(5)
def stopThread(self):
self.stopRequest.set()
def startWorker():
worker = Worker()
worker.doSomething()
# Start thread
t = threading.Thread(target=startWorker)
t.start()
# Stop thread
t.stopThread()
유용한 스레드 종료 토론 : http://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread-in-python – DyZ
당신은' 당신이 생각하기에 아마도 당신이 생각하는 것처럼 '노동자'하위 클래스. 예제는 [** _ 스레드 시작 및 중지 방법 _ **] (http://stackoverflow.com/questions/15729498/how-to-start-and-stop-thread)를 참조하십시오. – martineau