From f3921dd1588fafdc99f127d697c2ca25b4b2c443 Mon Sep 17 00:00:00 2001 From: Matthias Kestenholz Date: Thu, 9 Jan 2025 10:05:07 +0100 Subject: [PATCH] Rewrite the code to use multiprocessing --- CHANGELOG.rst | 3 +++ .../management/commands/process_imagefields.py | 12 +++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 61ce381..0135ca0 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -6,6 +6,9 @@ Change log Next version ~~~~~~~~~~~~ +- Rewrote ``process_imagefields`` to use the multiprocessing module, which + hopefully improves compatibility on macOS. + 0.21 (2024-12-09) ~~~~~~~~~~~~~~~~~ diff --git a/imagefield/management/commands/process_imagefields.py b/imagefield/management/commands/process_imagefields.py index 42de7e4..8874a53 100644 --- a/imagefield/management/commands/process_imagefields.py +++ b/imagefield/management/commands/process_imagefields.py @@ -1,5 +1,5 @@ +import multiprocessing as mp import sys -from concurrent.futures import ProcessPoolExecutor from fnmatch import fnmatch from functools import partial @@ -87,6 +87,8 @@ def _process_field(self, field, options): force=options.get("force"), ) + pool = mp.Pool() + fn = partial( _process_instance, field=field, @@ -94,9 +96,9 @@ def _process_field(self, field, options): force=options.get("force"), ) - with ProcessPoolExecutor() as executor: + try: for index, (instance, errors) in enumerate( - executor.map(fn, queryset.iterator(chunk_size=100)) + pool.imap(fn, queryset.iterator(chunk_size=100)) ): if errors: self.stderr.write("\n".join(errors)) @@ -111,6 +113,10 @@ def _process_field(self, field, options): instance._skip_generate_files = True instance.save() + finally: + pool.close() + pool.join() + self.stdout.write("\r|{}| {}/{}".format("*" * 50, count, count))