
    j5j                        d Z ddlmZ ddlZddlmZmZ ddlmZm	Z	 ddl
mZmZ ddlmZmZ  ed      Zdd	Zdd
Z G d de      ZddZ G d de      Zy)z"OCRmyPDF concurrency abstractions.    )annotationsN)ABCabstractmethod)CallableIterable)AnyTypeVar)NullProgressBarProgressBarTc                      y N )_args_kwargss     J/var/www/html/qr/venv/lib/python3.12/site-packages/ocrmypdf/_concurrent.py
_task_noopr      s    
    c                $    |j                          y r   )update)_resultpbars     r   _task_finished_noopr      s    KKMr   c                      e Zd ZdZ ej
                         ZeZdddZ	ddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ
e	 	 	 	 	 	 	 	 	 	 	 	 	 	 d	d       Zy)
ExecutorzAbstract concurrent executor.N
pbar_classc                   |r|| _         y y r   r   )selfr   s     r   __init__zExecutor.__init__    s    (DO r   )worker_initializertasktask_argumentstask_finishedc          
         |sy|st         }|st        }|st         }| j                  5  | j                  |||||||       ddd       y# 1 sw Y   yxY w)a  Set up parallel execution and progress reporting.

        Args:
            use_threads: If ``False``, the workload is the sort that will benefit from
                running in a multiprocessing context (for example, it uses Python
                heavily, and parallelizing it with threads is not expected to be
                performant).
            max_workers: The maximum number of workers that should be run.
            progress_kwargs: Arguments to set up the progress bar.
            worker_initializer: Called when a worker is initialized, in the worker's
                execution context. If the child workers are processes, it must be
                possible to marshall/pickle the worker initializer.
                ``functools.partial`` can be used to bind parameters.
            task: Called when the worker starts a new task, in the worker's execution
                context. Must be possible to marshall to the worker.
            task_finished: Called when a worker finishes a task, in the parent's
                context.
            task_arguments: An iterable that generates a group of parameters for each
                task. This runs in the parent's context, but the parameters must be
                marshallable to the worker.
        N)use_threadsmax_workersprogress_kwargsr!   r"   r#   r$   )r   r   	pool_lock_executer   r&   r'   r(   r!   r"   r#   r$   s           r   __call__zExecutor.__call__$   sf    @ !!+/MD^^ 		MM'' /#5-+  		 		 		s   A

Ac                    y)z-Custom executors should override this method.Nr   r+   s           r   r*   zExecutor._executeX   s    r   )r&   boolr'   intr(   dictr!   zCallable | Noner"   zCallable[..., T] | Noner#   zIterable | Noner$   z'Callable[[T, ProgressBar], None] | NonereturnNoner&   r.   r'   r/   r(   r0   r!   r   r"   r   r#   r   r$   r   )__name__
__module____qualname____doc__	threadingLockr)   r
   r   r    r,   r   r*   r   r   r   r   r      s    '	 I J%) ) /3(,*.AE2 2 	2
 2 ,2 &2 (2 ?2 
2h < < 	<
 < %< < !<  < <r   r   c                F    | j                         }| j                  |      S )N)progressbar_class)get_progressbar_classget_executor)plugin_managerr   s     r   setup_executorr?   g   s$    557J&&&DDr   c                  4    e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZy)SerialExecutorzImplements a purely sequential executor using the parallel protocol.

    The current process/thread will be the worker that executes all tasks
    in order. As such, ``worker_initializer`` will never be called.
    c               ~     | j                   di |5 }|D ]  }	 ||	 }
 ||
|        	 d d d        y # 1 sw Y   y xY w)Nr   r   )r   r&   r'   r(   r!   r"   r#   r$   r   argsresults              r   r*   zSerialExecutor._executes   sN     T__// 	,4& ,tfd+,	, 	, 	,s   3<Nr3   )r4   r5   r6   r7   r*   r   r   r   rA   rA   l   sR    , , 	,
 , %, , !,  ,r   rA   )r1   r2   )r   r   r   r   )r1   r   )r7   
__future__r   r8   abcr   r   collections.abcr   r   typingr   r	   ocrmypdf._progressbarr
   r   r   r   r   r   r?   rA   r   r   r   <module>rJ      sP    ) "  # .  >CLJ<s J<ZE
,X ,r   