Skip to content

Commit f3921dd

Browse files
committed
Rewrite the code to use multiprocessing
1 parent f80d27c commit f3921dd

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Change log
66
Next version
77
~~~~~~~~~~~~
88

9+
- Rewrote ``process_imagefields`` to use the multiprocessing module, which
10+
hopefully improves compatibility on macOS.
11+
912

1013
0.21 (2024-12-09)
1114
~~~~~~~~~~~~~~~~~

imagefield/management/commands/process_imagefields.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import multiprocessing as mp
12
import sys
2-
from concurrent.futures import ProcessPoolExecutor
33
from fnmatch import fnmatch
44
from functools import partial
55

@@ -87,16 +87,18 @@ def _process_field(self, field, options):
8787
force=options.get("force"),
8888
)
8989

90+
pool = mp.Pool()
91+
9092
fn = partial(
9193
_process_instance,
9294
field=field,
9395
housekeep=options.get("housekeep"),
9496
force=options.get("force"),
9597
)
9698

97-
with ProcessPoolExecutor() as executor:
99+
try:
98100
for index, (instance, errors) in enumerate(
99-
executor.map(fn, queryset.iterator(chunk_size=100))
101+
pool.imap(fn, queryset.iterator(chunk_size=100))
100102
):
101103
if errors:
102104
self.stderr.write("\n".join(errors))
@@ -111,6 +113,10 @@ def _process_field(self, field, options):
111113
instance._skip_generate_files = True
112114
instance.save()
113115

116+
finally:
117+
pool.close()
118+
pool.join()
119+
114120
self.stdout.write("\r|{}| {}/{}".format("*" * 50, count, count))
115121

116122

0 commit comments

Comments
 (0)