대기열에서 항목을 가져 와서 코드를 실행하는 클래스가 있습니다. 또한 처리를 위해 대기열에 항목을 추가하는 코드가 main 함수에 있습니다.스레드를 사용하여 프로그램을 올바르게 종료하는 방법은 무엇입니까?
어떤 이유로 프로그램이 제대로 끝내기를 원하지 않습니다. 는 queue.join()
및 인쇄 Done!
에서 제대로 완료 될 때까지
class Downloader(Thread):
def __init__(self, queue):
self.queue = queue
Thread.__init__(self)
def run(self):
while True:
download_file(self.queue.get())
self.queue.task_done()
def spawn_threads(Class, amount):
for t in xrange(amount):
thread = Class(queue)
thread.setDaemon = True
thread.start()
if __name__ == "__main__":
spawn_threads(Downloader, 20)
for item in items: queue.put(item)
#not the real code, but simplied because it isn't relevant
print 'Done scanning. Waiting for downloads to finish.'
queue.join()
print 'Done!'
이 프로그램은 대기하지만, 뭔가 내가 내 손가락을 넣어 수없는 것 종가에서 프로그램을 계속 : 여기
는 코드입니다. 나는 그것이while True
루프라고 생각 하겠지만, 스레드를 데몬으로 설정하는 것은 그것을 해결하기위한 것이라고 생각했습니다.
'while True' w 병이 무한대로 계속 실행되면 스레드를 중지하고 스레드를 종료해야합니다. – andrean