o
    *j                  	   @   sn   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dfde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_descfault_tolerantc                    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           
         s0  g }t dddt| pdt|  ddr}td] fdd	| D }g }t|D ]B}|d
 rjz	||  W q/ tyi } z|| }	t	d|	 d|  ||	|f W Y d }~q/d }~ww ||  q/W d    n1 s|w   Y  W d    n1 sw   Y  r||fS |S )NTi   r   zProcessing z items)Z
unit_scaleZunit_divisorinitialtotaldescdisable)r	   c                    s(   i | ]}j |g R i |qS  )Zsubmit).0item)argsexecutorfunckwargsr   ^/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/utils/thread_utils.py
<dictcomp>;   s    zGthread_executor.<locals>.decorator.<locals>.wrapper.<locals>.<dictcomp>   zTask failed for z: )
r   lenr   r   updateappendresult	Exceptionloggererror)
iterabler   r   resultsZpbarZfuturesZfailed_itemsfutureer   )r
   r   r   r	   r   )r   r   r   r   wrapper,   sD   
z3thread_executor.<locals>.decorator.<locals>.wrapperr   )r   r&   r
   r   r	   r   )r   r   	decorator*   s   $z"thread_executor.<locals>.decoratorr   )r	   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   r    intboolstrr)   r   r   r   r   <module>   s"   