From 0c328bf07a5ddc44fad5bfd51b61bc77cd9cf04c Mon Sep 17 00:00:00 2001 From: Richard Tibbles Date: Sun, 3 Nov 2024 09:20:58 -0800 Subject: [PATCH] Refactor multiprocessing compatibility into general kolibri utils module. --- kolibri/core/tasks/test/taskrunner/conftest.py | 10 +++++----- kolibri/core/tasks/test/taskrunner/test_job_running.py | 2 +- kolibri/core/tasks/utils.py | 6 +++--- kolibri/core/tasks/worker.py | 2 +- .../compat.py => utils/multiprocessing_compat.py} | 0 5 files changed, 10 insertions(+), 10 deletions(-) rename kolibri/{core/tasks/compat.py => utils/multiprocessing_compat.py} (100%) diff --git a/kolibri/core/tasks/test/taskrunner/conftest.py b/kolibri/core/tasks/test/taskrunner/conftest.py index 86f6b93cf19..451776f999b 100644 --- a/kolibri/core/tasks/test/taskrunner/conftest.py +++ b/kolibri/core/tasks/test/taskrunner/conftest.py @@ -1,6 +1,6 @@ import pytest -from kolibri.core.tasks import compat +from kolibri.utils import multiprocessing_compat @pytest.fixture(params=[False, True], autouse=True) @@ -24,7 +24,7 @@ class local(object): from threading import local # noqa from concurrent.futures import ThreadPoolExecutor as PoolExecutor # noqa - monkeypatch.setattr(compat, "Thread", Thread) - monkeypatch.setattr(compat, "Event", Event) - monkeypatch.setattr(compat, "local", local) - monkeypatch.setattr(compat, "PoolExecutor", PoolExecutor) + monkeypatch.setattr(multiprocessing_compat, "Thread", Thread) + monkeypatch.setattr(multiprocessing_compat, "Event", Event) + monkeypatch.setattr(multiprocessing_compat, "local", local) + monkeypatch.setattr(multiprocessing_compat, "PoolExecutor", PoolExecutor) diff --git a/kolibri/core/tasks/test/taskrunner/test_job_running.py b/kolibri/core/tasks/test/taskrunner/test_job_running.py index d07d82baef8..b5c6a7c640c 100644 --- a/kolibri/core/tasks/test/taskrunner/test_job_running.py +++ b/kolibri/core/tasks/test/taskrunner/test_job_running.py @@ -3,7 +3,6 @@ import pytest -from kolibri.core.tasks.compat import Event from kolibri.core.tasks.exceptions import JobNotFound from kolibri.core.tasks.job import Job from kolibri.core.tasks.job import State @@ -13,6 +12,7 @@ from kolibri.core.tasks.utils import get_current_job from kolibri.core.tasks.utils import import_path_to_callable from kolibri.core.tasks.worker import Worker +from kolibri.utils.multiprocessing_compat import Event @pytest.fixture diff --git a/kolibri/core/tasks/utils.py b/kolibri/core/tasks/utils.py index 4fd488f65c1..cb9d2694394 100644 --- a/kolibri/core/tasks/utils.py +++ b/kolibri/core/tasks/utils.py @@ -15,9 +15,9 @@ from kolibri.core.sqlite.utils import check_sqlite_integrity from kolibri.core.sqlite.utils import repair_sqlite_db -from kolibri.core.tasks import compat from kolibri.core.tasks.exceptions import UserCancelledError from kolibri.utils import conf +from kolibri.utils import multiprocessing_compat from kolibri.utils.options import FD_PER_THREAD from kolibri.utils.system import get_fd_limit @@ -27,7 +27,7 @@ # An object on which to store data about the current job # So far the only use is to track the job, but other metadata # could be added. -current_state_tracker = SimpleLazyObject(compat.local) +current_state_tracker = SimpleLazyObject(multiprocessing_compat.local) def get_current_job(): @@ -76,7 +76,7 @@ def __init__(self, func, thread_name, wait_between_runs=1, *args, **kwargs): :param thread_name: the name of the thread to use during logging and debugging :param wait_between_runs: how many seconds to wait in between func calls. """ - self.shutdown_event = compat.Event() + self.shutdown_event = multiprocessing_compat.Event() self.thread_name = thread_name self.thread_id = uuid.uuid4().hex self.logger = logging.getLogger( diff --git a/kolibri/core/tasks/worker.py b/kolibri/core/tasks/worker.py index f2b9ce4ccec..fa2e3bfe385 100644 --- a/kolibri/core/tasks/worker.py +++ b/kolibri/core/tasks/worker.py @@ -3,11 +3,11 @@ from django.db import connection as django_connection -from kolibri.core.tasks.compat import PoolExecutor from kolibri.core.tasks.constants import Priority from kolibri.core.tasks.storage import Storage from kolibri.core.tasks.utils import db_connection from kolibri.core.tasks.utils import InfiniteLoopThread +from kolibri.utils.multiprocessing_compat import PoolExecutor logger = logging.getLogger(__name__) diff --git a/kolibri/core/tasks/compat.py b/kolibri/utils/multiprocessing_compat.py similarity index 100% rename from kolibri/core/tasks/compat.py rename to kolibri/utils/multiprocessing_compat.py