From 63bc69560047abb23d7a4f9aa4bf61491c166eaf Mon Sep 17 00:00:00 2001 From: Tilman Kamp <5991088+tilmankamp@users.noreply.github.com> Date: Thu, 12 Mar 2020 14:34:24 +0100 Subject: [PATCH] Process pool for audio preparation --- util/audio.py | 11 +++++++---- util/helpers.py | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/util/audio.py b/util/audio.py index cad596d7..e2469e8a 100644 --- a/util/audio.py +++ b/util/audio.py @@ -92,12 +92,15 @@ class Sample: self.audio_type = new_audio_type +def _change_audio_type(sample_and_audio_type): + sample, audio_type = sample_and_audio_type + sample.change_audio_type(audio_type) + return sample + + def change_audio_types(samples, audio_type=AUDIO_TYPE_PCM, processes=None, process_ahead=None): - def change_audio_type(sample): - sample.change_audio_type(audio_type) - return sample with LimitingPool(processes=processes, process_ahead=process_ahead) as pool: - yield from pool.imap(change_audio_type, samples) + yield from pool.imap(_change_audio_type, map(lambda s: (s, audio_type), samples)) def read_audio_format_from_wav_file(wav_file): diff --git a/util/helpers.py b/util/helpers.py index 73cd9b74..c158066d 100644 --- a/util/helpers.py +++ b/util/helpers.py @@ -4,7 +4,7 @@ import time import heapq import semver -from multiprocessing.dummy import Pool as ThreadPool +from multiprocessing import Pool KILO = 1024 KILOBYTE = 1 * KILO @@ -83,7 +83,7 @@ class LimitingPool: self.process_ahead = os.cpu_count() if process_ahead is None else process_ahead self.sleeping_for = sleeping_for self.processed = 0 - self.pool = ThreadPool(processes=processes) + self.pool = Pool(processes=processes) def __enter__(self): return self