o
    rqiV	                     @   sh   d dl mZmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 e
 Zeddfded	ed
efddZdS )    )ThreadPoolExecutoras_completedwraps)tqdm)DEFAULT_MAX_WORKERS)
get_loggerFNmax_workersdisable_tqdm	tqdm_descc                    s    fdd}|S )a  
    A decorator to execute a function in a threaded manner using ThreadPoolExecutor.

    Args:
        max_workers (int): The maximum number of threads to use.
        disable_tqdm (bool): disable progress bar.
        tqdm_desc (str): Desc of tqdm.

    Returns:
        function: A wrapped function that executes with threading and a progress bar.

    Examples:
        >>> from modelscope.utils.thread_utils import thread_executor
        >>> import time
        >>> @thread_executor(max_workers=8)
        ... def process_item(item, x, y):
        ...     # do something to single item
        ...     time.sleep(1)
        ...     return str(item) + str(x) + str(y)

        >>> items = [1, 2, 3]
        >>> process_item(items, x='abc', y='xyz')
    c                    s   t   fdd}|S )Nc              	      s   g }t dddt| pdt|  ddE}td' fdd	| D }t|D ]}|d
 ||  q-W d    n1 sFw   Y  W d    |S W d    |S 1 s^w   Y  |S )NTi   r   zProcessing z items)Z
unit_scaleZunit_divisorinitialtotalZdescdisable)r	   c                    s(   i | ]}j |g R i |qS  )submit).0item)argsexecutorfunckwargsr   d/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/modelscope/utils/thread_utils.py
<dictcomp>:   s    zGthread_executor.<locals>.decorator.<locals>.wrapper.<locals>.<dictcomp>   )r   lenr   r   updateappendresult)iterabler   r   resultsZpbarfuturesfuture)r
   r   r	   r   )r   r   r   r   wrapper+   s4   


z3thread_executor.<locals>.decorator.<locals>.wrapperr   )r   r"   r
   r	   r   )r   r   	decorator)   s   z"thread_executor.<locals>.decoratorr   )r	   r
   r   r$   r   r#   r   thread_executor   s   r%   )concurrent.futuresr   r   	functoolsr   Z	tqdm.autor   Zmodelscope.hub.constantsr   Zmodelscope.utils.loggerr   loggerintboolstrr%   r   r   r   r   <module>   s   