Skip to content

Commit 9087c8e

Browse files
committed
Introduce a --no-parallel argument
1 parent f3921dd commit 9087c8e

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Next version
88

99
- Rewrote ``process_imagefields`` to use the multiprocessing module, which
1010
hopefully improves compatibility on macOS.
11+
- Introduced a ``--no-parallel`` argument to ``process_imagefields``.
1112

1213

1314
0.21 (2024-12-09)

imagefield/management/commands/process_imagefields.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ def add_arguments(self, parser):
2525
default="",
2626
help="Run house-keeping tasks.",
2727
)
28+
parser.add_argument(
29+
"--no-parallel",
30+
action="store_true",
31+
dest="no_parallel",
32+
help="Process images sequentially without parallelization.",
33+
)
2834
parser.add_argument(
2935
"field",
3036
nargs="*",
@@ -87,18 +93,16 @@ def _process_field(self, field, options):
8793
force=options.get("force"),
8894
)
8995

90-
pool = mp.Pool()
91-
9296
fn = partial(
9397
_process_instance,
9498
field=field,
9599
housekeep=options.get("housekeep"),
96100
force=options.get("force"),
97101
)
98102

99-
try:
103+
if options.get("no_parallel"):
100104
for index, (instance, errors) in enumerate(
101-
pool.imap(fn, queryset.iterator(chunk_size=100))
105+
map(fn, queryset.iterator(chunk_size=100))
102106
):
103107
if errors:
104108
self.stderr.write("\n".join(errors))
@@ -112,10 +116,28 @@ def _process_field(self, field, options):
112116
# if not done already
113117
instance._skip_generate_files = True
114118
instance.save()
115-
116-
finally:
117-
pool.close()
118-
pool.join()
119+
else:
120+
pool = mp.Pool()
121+
try:
122+
for index, (instance, errors) in enumerate(
123+
pool.imap(fn, queryset.iterator(chunk_size=100))
124+
):
125+
if errors:
126+
self.stderr.write("\n".join(errors))
127+
128+
progress = "*" * (50 * index // count)
129+
self.stdout.write(
130+
f"\r|{progress.ljust(50)}| {index + 1}/{count}", ending=""
131+
)
132+
133+
# Save instance once for good measure; fills in width/height
134+
# if not done already
135+
instance._skip_generate_files = True
136+
instance.save()
137+
138+
finally:
139+
pool.close()
140+
pool.join()
119141

120142
self.stdout.write("\r|{}| {}/{}".format("*" * 50, count, count))
121143

0 commit comments

Comments
 (0)