キューからアイテムを取得してコードを実行するクラスがあります。メイン関数には、処理のためにアイテムをキューに追加するコードもあります。
何らかの理由で、プログラムが正常に終了したくありません。
コードは次のとおりです。
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!'
プログラムは で適切に終了するのを待ち、queue.join()
を出力Done!
しますが、何かが原因でプログラムが閉じられず、指を置くことができないようです。ループだと思いwhile True
ますが、スレッドをデーモンとして設定することはそれを解決するためのものだと思いました。