o
    j&                  
   @   s   d Z ddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 dd	giZg d
ZG dd dZed!ddZdd Zdd Zddddeddddd	ddZdd Zdd Zdddd ZdS )"z,
Thin wrappers around `concurrent.futures`.
    N)contextmanagerlength_hint   )tqdm)TqdmWarningzgithub.com/	casperdcl)
thread_mapprocess_mapinterpreter_mapc                   @   sR   e Zd ZdZddlmZ ddlmZ dd Z	dd	d
Z
dd Zdd Zdd ZdS )_InterpreterLockz3Reentrant lock backed by a cross-interpreter queue.r   )	get_ident)	monotonicc                 C   s*   ddl m} || _| | _d | _d| _d S )Nr   )RLock)	threadingr   _queue_lock_owner_depth)selfqueuer    r   /root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/tqdm/contrib/concurrent.py__init__   s
   
z_InterpreterLock.__init__Tc                 C   s   ddl m} |  }|dkr| j|}n| j||}|s dS | jr,|  jd7  _dS z&|s5| j  n|dkr?| j  nt	d||  |  }| jj|d W n |ya   | j
  Y dS w |  | _d| _dS )Nr   )Emptyr   F   T)timeout)r   r   _timer   acquirer   r   
get_nowaitgetmaxreleaser   r   )r   blockingr   r   startacquired	remainingr   r   r   r      s2   

z_InterpreterLock.acquirec                 C   sJ   | j |  krtd|  jd8  _| jsd | _ | jd  | j  d S )Nzcannot release un-acquired lockr   )r   r   RuntimeErrorr   r   putr   r#   r   r   r   r   r#   6   s   z_InterpreterLock.releasec                 C   s   |    | S N)r   r*   r   r   r   	__enter__?   s   z_InterpreterLock.__enter__c                 G   s   |    d S r+   )r#   )r   excr   r   r   __exit__C   s   z_InterpreterLock.__exit__N)Tr   )__name__
__module____qualname____doc__r   r   timer   r   r   r   r#   r,   r.   r   r   r   r   r      s    
	r    c                 c   s\    t | dd}|du r|p|  }t |||}| | |V  |du r'| `dS | | dS )z>get (create if necessary) and then restore `tqdm_class`'s lockr   N)getattrget_lockset_lockr   )
tqdm_class	lock_namelockold_lockr   r   r   ensure_lockG   s   
r<   c              	   C   s4   dt jd| jd| jdd|d	}t|ffS )zGReturn an initializer which bootstraps the parent import path and lock.zimport sys
sys.path[:] = z
from concurrent import interpreters
from importlib import import_module
from tqdm.contrib.concurrent import _InterpreterLock
tqdm_class = import_module(z)
for name in .z:
    tqdm_class = getattr(tqdm_class, name)
tqdm_class.monitor_interval = 0
tqdm_class.set_lock(_InterpreterLock(interpreters.Queue(z))))syspathr0   r1   splitexec)r8   lock_queue_idcoder   r   r   _get_interpreter_initV   s   
	
rD   c                    s   t  fdd| D ddS )z+min(map(length_hint, iterables), default=0)c                 3   s&    | ]}t |d   dkr V  qdS )r   r   Nr   ).0itnr   r   	<genexpr>h   s   $ z_min_map_len.<locals>.<genexpr>r   )default)min)	iterablesr   rG   r   _min_map_lenf   s   rM   r   g        )	max_workersr   	chunksizer9   r8   	smoothingr   _initializer	_initargsc       	      
      s  |  }d|vrt||d< i }d|v r|d|d< i }dD ]}||v r,||||< qd}|d rgd|vrgzddlm} W n tyN   ddlm} Y nw |pZtd	| pWd
d }|d |krg||d< d}t|||d}|	du ry|j	}	|f}
| d||	|
d|T}|dd|i|;|durd_
|j  fdd}||_t|j|g|R ||d|W  d   W  d   W  d   S 1 sw   Y  W d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  dS )a  
    Implementation of `thread_map`, `process_map` and `interpreter_map`.

    Parameters
    ----------
    max_workers  : int
    timeout  : int
    buffersize  : int
        Requires Python>=3.14.
    thread_name_prefix  : str
    max_tasks_per_child  : int
    mp_context  : str
    total
buffersize)thread_name_prefixmax_tasks_per_child
mp_contextNminitersr   )process_cpu_count)	cpu_count    r      T)r9   r:   )rN   initializerinitargsrP   c                     s$    | i |}| fdd |S )Nc                    s      S r+   )update)_)pbarr   r   <lambda>   s    z4_executor_map.<locals>.patchsubmit.<locals>.<lambda>)add_done_callback)argskwargsfut	orisubmitra   r   r   patchsubmit   s   z"_executor_map.<locals>.patchsubmit)r   rO   r   )copyrM   poposrY   ImportErrorrZ   rK   r<   r7   dynamic_miniterssubmitlistmap)PoolExecutorfnrN   r   rO   r9   r8   rP   r   rQ   rR   rL   tqdm_kwargsre   
map_kwargspool_kwargskrn   rZ   	rough_maxlkexri   r   rg   r   _executor_mapk   sj   "r{   c                 O   s$   ddl m} t|| g|R i |S )a  
    Equivalent of `list(map(fn, *iterables))`
    driven by `concurrent.futures.ThreadPoolExecutor`.

    Parameters
    ----------
    max_workers  : int, optional
        Maximum number of workers to spawn; passed to `concurrent.futures.ThreadPoolExecutor`.
    thread_name_prefix  : str, optional
        Passed to `concurrent.futures.ThreadPoolExecutor` [default: ''].
    timeout  : int or float, optional
        Seconds to wait before raising `TimeoutError` if `__next__` is called and the
        result isn't available. [default: None].
    buffersize  : int, optional
        Requires Python>=3.14 [default: None].
    tqdm_class  : optional
        `tqdm` class to use for bars [default: tqdm.auto.tqdm].
    smoothing  : float, optional
        Passed to `tqdm_class`; the [default: 0] is average (due to erratic update frequency).
    lock_name  : str, optional
        Member of `tqdm_class.get_lock()` to use [default: ''].
    r   )ThreadPoolExecutor)concurrent.futuresr|   r{   )rs   rL   rt   r|   r   r   r   r	      s   r	   c           	      O   sj   ddl m} ddlm} | }|d |dt}t||j	\}}t
|| g|R t|||d|S )aj  
    Equivalent of `list(map(fn, *iterables))`
    driven by `concurrent.futures.InterpreterPoolExecutor` (Python 3.14+).

    Parameters
    ----------
    Same as `thread_map`.

    Notes
    -----
    `fn`, its arguments, and its return values must be pickleable.
    Worker progress bars using the same `tqdm_class` share a cross-interpreter write lock.
    r   )interpreters)InterpreterPoolExecutorNr8   )r   rQ   rR   )
concurrentr~   r}   r   create_queuer)   r!   	tqdm_autorD   idr{   r   )	rs   rL   rt   r~   r   
lock_queuer8   r]   r^   r   r   r   r      s    
r   mp_lock)r9   c                O   sb   ddl m} |r#d|vr#t|}|dkr#ddlm} |d| tdd t|| g|R d	|i|S )
aI  
    Equivalent of `list(map(fn, *iterables))`
    driven by `concurrent.futures.ProcessPoolExecutor`.

    Parameters
    ----------
    max_workers  : int, optional
        Maximum number of workers to spawn; passed to `concurrent.futures.ProcessPoolExecutor`.
    timeout  : int or float, optional
        Seconds to wait before raising `TimeoutError` if `__next__` is called and the
        result isn't available. [default: None].
    chunksize  : int, optional
        Approximate size of chunks sent to worker processes; passed to
        `concurrent.futures.ProcessPoolExecutor.map`. [default: 1].
    buffersize  : int, optional
        Requires Python>=3.14 [default: None].
    max_tasks_per_child  : int, optional
        Maximum number of tasks a worker process can complete before being replaced
        with a new process; passed to `concurrent.futures.ProcessPoolExecutor`.
    mp_context  : multiprocessing.BaseContext, optional
        Multiprocessing context to use, e.g. `multiprocessing.get_context('fork')`.
    lock_name  : str, optional
        Member of `tqdm_class.get_lock()` to use [default: mp_lock].
    tqdm_class  : optional
        `tqdm` class to use for bars [default: tqdm.auto.tqdm].
    smoothing  : float, optional
        Passed to `tqdm_class`; the [default: 0] is average (due to erratic update frequency).
    r   )ProcessPoolExecutorrO   i  )warnzIterable length %d > 1000 but `chunksize` is not set. This may seriously degrade multiprocess performance. Set `chunksize=1` or more.r   )
stacklevelr9   )r}   r   rM   warningsr   r   r{   )rs   r9   rL   rt   r   shortest_iterable_lenr   r   r   r   r
      s   r
   )r4   N)r2   r>   
contextlibr   operatorr   autor   r   stdr   
__author____all__r   r<   rD   rM   r{   r	   r   r
   r   r   r   r   <module>   s&    
8
<