def start(self): print 'Start Speech Download Service...' language = self._language download_dir = self.setup_download_dir() # Create a queue to communicate with the worker threads queue = Queue() with open(self._text_file_path, 'r') as f: progress = Progress(len(f.readlines())) f.seek(0) queue.count = 0 queue.progress = progress for text in f: text = text.rstrip('\n') encoded_args = urlencode({ 'hl': language, 'src': text, 'key': config.API_KEY }) url = config.SPEECH_URL + encoded_args download_path = os.path.join(download_dir, text + config.DOWNLOAD_FILE_TYPE) # queue accepts only one object queue.put( (url, download_path) ) # Create 8 worker threads for _ in range(self.THREAD_POOL_SIZE): thread = SpeechDownloadThread(queue) # Setting daemon to true will let the main thread exit # even though the workers are blocking thread.daemon = True thread.start() # Block until all tasks are done queue.join() print 'Done, Downloaded %d Speeches' % queue.count