Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

parallel: Refactor to provide a callback form.

Alex Vandiverc15f426dc6fc
diff --git a/zerver/lib/parallel.py b/zerver/lib/parallel.py
index 561c73f326d..7efd4a79344 100644
--- a/zerver/lib/parallel.py
+++ b/zerver/lib/parallel.py
@@ -1,6 +1,7 @@
import logging
-from collections.abc import Callable, Iterable
-from concurrent.futures import ProcessPoolExecutor, as_completed
+from collections.abc import Callable, Iterable, Iterator
+from concurrent.futures import BrokenExecutor, Future, ProcessPoolExecutor
+from contextlib import contextmanager
from multiprocessing import current_process
from typing import Any, TypeVar
@@ -48,30 +49,79 @@ def run_parallel(
catch: bool = False,
report_every: int = 1000,
report: Callable[[int], None] | None = None,
-) -> None: # nocoverage
+) -> None:
+ with run_parallel_queue(
+ func,
+ processes,
+ initializer=initializer,
+ initargs=initargs,
+ catch=catch,
+ report_every=report_every,
+ report=report,
+ ) as submit:
+ for record in records:
+ submit(record)
+
+
+@contextmanager
+def run_parallel_queue(
+ func: Callable[[ParallelRecordType], None],
+ processes: int,
+ *,
+ initializer: Callable[..., None] | None = None,
+ initargs: tuple[Any, ...] = tuple(),
+ catch: bool = False,
+ report_every: int = 1000,
+ report: Callable[[int], None] | None = None,
+) -> Iterator[Callable[[ParallelRecordType], None]]:
assert processes > 0
- if settings.TEST_SUITE and current_process().daemon:
+ if settings.TEST_SUITE and current_process().daemon: # nocoverage
assert processes == 1, "Only one process possible under parallel tests"
wrapped_func = partial(func_with_catch, func) if catch else func
+ completed = 0
if processes == 1:
+
+ def func_with_notify(item: ParallelRecordType) -> None:
+ wrapped_func(item)
+ nonlocal completed
+ completed += 1
+ if report is not None and completed % report_every == 0:
+ report(completed)
+
if initializer is not None:
initializer(*initargs)
- for count, record in enumerate(records, 1):
- wrapped_func(record)
- if report is not None and count % report_every == 0:
- report(count)
+ yield func_with_notify
return
- _disconnect()
-
- with ProcessPoolExecutor(
- max_workers=processes, initializer=initializer, initargs=initargs
- ) as executor:
- for count, future in enumerate(
- as_completed(executor.submit(wrapped_func, record) for record in records), 1
- ):
- future.result()
- if report is not None and count % report_every == 0:
- report(count)
+ else: # nocoverage
+ _disconnect()
+
+ exceptions = []
+ try:
+ with ProcessPoolExecutor(
+ max_workers=processes, initializer=initializer, initargs=initargs
+ ) as executor:
+
+ def report_callback(future: Future[None]) -> None:
+ if exc := future.exception():
+ exceptions.append(exc)
+ return
+
+ nonlocal completed
+ completed += 1
+ if report is not None and completed % report_every == 0:
+ report(completed)
+
+ def future_with_notify(item: ParallelRecordType) -> None:
+ if exceptions:
+ executor.shutdown(cancel_futures=True)
+ raise BrokenExecutor
+ future = executor.submit(wrapped_func, item)
+ future.add_done_callback(report_callback)
+
+ yield future_with_notify
+ finally:
+ if exceptions:
+ raise exceptions[0]
diff --git a/zerver/tests/test_parallel.py b/zerver/tests/test_parallel.py
index d62952545a3..d6419f48d00 100644
--- a/zerver/tests/test_parallel.py
+++ b/zerver/tests/test_parallel.py
@@ -9,7 +9,7 @@ from multiprocessing import current_process
from django.db import connection
-from zerver.lib.parallel import _disconnect, run_parallel
+from zerver.lib.parallel import _disconnect, run_parallel, run_parallel_queue
from zerver.lib.partial import partial
from zerver.lib.test_classes import ZulipTestCase
from zerver.models import Realm
@@ -120,6 +120,39 @@ class RunNotParallelTest(ZulipTestCase):
],
)
+ def test_not_parallel_queue(self) -> None:
+ events = []
+
+ with run_parallel_queue(
+ lambda item: events.append(f"Item: {item}"),
+ processes=1,
+ report_every=3,
+ report=lambda n: events.append(f"Completed {n}"),
+ initializer=lambda a, b: events.append(f"Init: {a}, {b}"),
+ initargs=("alpha", "bravo"),
+ ) as do_work:
+ self.assertEqual(events, ["Init: alpha, bravo"])
+ do_work(100)
+ self.assertEqual(
+ events,
+ [
+ "Init: alpha, bravo",
+ "Item: 100",
+ ],
+ )
+ do_work(101)
+ do_work(102)
+ self.assertEqual(
+ events,
+ [
+ "Init: alpha, bravo",
+ "Item: 100",
+ "Item: 101",
+ "Item: 102",
+ "Completed 3",
+ ],
+ )
+
def write_number(
output_dir: str, total_processes: int, fail: set[int], item: int