diff --git a/src/ocrmypdf/_concurrent.py b/src/ocrmypdf/_concurrent.py index 87c9656c..e3fb5b91 100644 --- a/src/ocrmypdf/_concurrent.py +++ b/src/ocrmypdf/_concurrent.py @@ -4,8 +4,11 @@ # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. +import threading from typing import Callable, Iterable, Optional +pool_lock = threading.Lock() + def _task_noop(*_args, **_kwargs): return @@ -35,12 +38,13 @@ def exec_progress_pool( if not task_finished: task_finished = _task_noop - _model( - use_threads=use_threads, - max_workers=max_workers, - tqdm_kwargs=tqdm_kwargs, - worker_initializer=worker_initializer, - task=task, - task_arguments=task_arguments, - task_finished=task_finished, - ) + with pool_lock: + _model( + use_threads=use_threads, + max_workers=max_workers, + tqdm_kwargs=tqdm_kwargs, + worker_initializer=worker_initializer, + task=task, + task_arguments=task_arguments, + task_finished=task_finished, + ) diff --git a/src/ocrmypdf/builtin_plugins/concurrency.py b/src/ocrmypdf/builtin_plugins/concurrency.py index 7d7fbbfc..3b3e0d74 100644 --- a/src/ocrmypdf/builtin_plugins/concurrency.py +++ b/src/ocrmypdf/builtin_plugins/concurrency.py @@ -31,8 +31,6 @@ from ocrmypdf.exceptions import InputFileError Queue = Union[multiprocessing.Queue, queue.Queue] -pool_lock = threading.Lock() - def log_listener(q: Queue): """Listen to the worker processes and forward the messages to logging @@ -120,18 +118,17 @@ def exec_progress_pool( worker_initializer = _noop - with pool_lock: - _exec_progress_pool( - max_workers=max_workers, - tqdm_kwargs=tqdm_kwargs, - worker_initializer=worker_initializer, - task=task, - task_arguments=task_arguments, - task_finished=task_finished, - log_queue=log_queue, - pool_class=pool_class, - initializer=initializer, - ) + _exec_progress_pool( + max_workers=max_workers, + tqdm_kwargs=tqdm_kwargs, + worker_initializer=worker_initializer, + task=task, + task_arguments=task_arguments, + task_finished=task_finished, + log_queue=log_queue, + pool_class=pool_class, + initializer=initializer, + ) def _exec_progress_pool( diff --git a/src/ocrmypdf/extra_plugins/awslambda.py b/src/ocrmypdf/extra_plugins/awslambda.py index 3a5f802d..4f7a7af7 100644 --- a/src/ocrmypdf/extra_plugins/awslambda.py +++ b/src/ocrmypdf/extra_plugins/awslambda.py @@ -11,7 +11,6 @@ import logging import logging.handlers import signal -import threading from contextlib import suppress from enum import Enum, auto from itertools import islice, repeat, takewhile, zip_longest @@ -23,8 +22,6 @@ from unittest.mock import Mock from ocrmypdf import hookimpl from ocrmypdf.exceptions import InputFileError -pool_lock = threading.Lock() - class MessageType(Enum): exception = auto() @@ -101,15 +98,14 @@ def lambda_pool_impl( task_finished(result, pbar) return - with pool_lock: - _lambda_pool_impl( - max_workers=max_workers, - worker_initializer=worker_initializer, - task=task, - task_arguments=task_arguments, - task_finished=task_finished, - pbar=pbar, - ) + _lambda_pool_impl( + max_workers=max_workers, + worker_initializer=worker_initializer, + task=task, + task_arguments=task_arguments, + task_finished=task_finished, + pbar=pbar, + ) def _lambda_pool_impl(