o
    e                     @  s|   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 Z
G dd	 d	ZG d
d deZdddZG dd deZdS )z"OCRmyPDF concurrency abstractions.    )annotationsN)ABCabstractmethod)CallableIterablec                  O     d S N )_args_kwargsr	   r	   DD:\Projects\ConvertPro\env\Lib\site-packages\ocrmypdf/_concurrent.py
_task_noop      r   c                   @  s2   e Zd ZdZdd Zdd Zdd Zdd	d
ZdS )NullProgressBarz'Progress bar API that takes no actions.c                 K  r   r   r	   )selfkwargsr	   r	   r   __init__   r   zNullProgressBar.__init__c                 C  s   | S r   r	   )r   r	   r	   r   	__enter__   r   zNullProgressBar.__enter__c                 C     dS )NFr	   )r   exc_type	exc_value	tracebackr	   r	   r   __exit__   r   zNullProgressBar.__exit__Nc                 C  r   r   r	   )r   Z_argr	   r	   r   update   r   zNullProgressBar.updater   )__name__
__module____qualname____doc__r   r   r   r   r	   r	   r	   r   r      s    r   c                   @  sN   e Zd ZdZe ZeZddddZ	ddddddddZ
edddZdS )ExecutorzAbstract concurrent executor.N
pbar_classc                C  s   |r|| _ d S d S r   r   )r   r    r	   r	   r   r   '   s   
zExecutor.__init__)worker_initializertasktask_argumentstask_finisheduse_threadsboolmax_workersinttqdm_kwargsdictr!   Callable | Noner"   r#   Iterable | Noner$   returnNonec             
   C  sd   |sdS |st }|st }|st }| j | j|||||||d W d   dS 1 s+w   Y  dS )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.
            tdqm_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)r%   r'   r)   r!   r"   r#   r$   )r   	pool_lock_executer   r%   r'   r)   r!   r"   r#   r$   r	   r	   r   __call__+   s&   ""zExecutor.__call__r   r   c                C  r   )z-Custom executors should override this method.Nr	   r1   r	   r	   r   r0   a   s    zExecutor._execute)r%   r&   r'   r(   r)   r*   r!   r+   r"   r+   r#   r,   r$   r+   r-   r.   r%   r&   r'   r(   r)   r*   r!   r   r"   r   r#   r   r$   r   )r   r   r   r   	threadingLockr/   r   r    r   r2   r   r0   r	   r	   r	   r   r   !   s    
6r   r-   c                 C  s   | j  }| j j|dS )N)Zprogressbar_class)hookZget_progressbar_classZget_executor)Zplugin_managerr    r	   r	   r   setup_executorp   s   
r7   c                   @  s   e Zd ZdZdddZdS )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.
    r%   r&   r'   r(   r)   r*   r!   r   r"   r#   r   r$   c                C  sR   | j di |}|D ]}	||	}
||
| qW d    d S 1 s"w   Y  d S )Nr	   r   )r   r%   r'   r)   r!   r"   r#   r$   Zpbarargsresultr	   r	   r   r0   |   s   "zSerialExecutor._executeNr3   )r   r   r   r   r0   r	   r	   r	   r   r8   u   s    r8   )r-   r   )r   
__future__r   r4   abcr   r   typingr   r   r   r   r   r7   r8   r	   r	   r	   r   <module>   s   
O