Ë
    Bü´j‘&  ã            
       ó¸   — 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«      Zedd„«       Zd„ Zd„ Zddddedddddœ	d„Zd„ Zd„ Zddœd„Zy)z,
Thin wrappers around `concurrent.futures`.
é    N)Úcontextmanager©Úlength_hinté   )Útqdm)ÚTqdmWarningzgithub.com/Ú	casperdcl)Ú
thread_mapÚprocess_mapÚinterpreter_mapc                   óH   — e Zd ZdZddlmZ ddlmZ d„ Z	dd„Z
d„ Zd„ Zd	„ Zy
)Ú_InterpreterLockz3Reentrant lock backed by a cross-interpreter queue.r   )Ú	get_ident)Ú	monotonicc                 óR   — ddl m} || _         |«       | _        d | _        d| _        y )Nr   )ÚRLock)Ú	threadingr   Ú_queueÚ_lockÚ_ownerÚ_depth)ÚselfÚqueuer   s      úU/root/workspace/ytshorts/venv/lib/python3.12/site-packages/tqdm/contrib/concurrent.pyÚ__init__z_InterpreterLock.__init__   s#   € Ý#ØˆŒÙ“WˆŒ
ØˆŒØˆ�ó    c                 ój  — ddl m} | j                  «       }|dk(  r| j                  j	                  |«      }n| j                  j	                  ||«      }|sy| j
                  r| xj
                  dz  c_        y	 |s| j                  j                  «        n\|dk(  r| j                  j                  «        n<t        d|| j                  «       |z
  z
  «      }| j                  j                  |¬«       | j                  «       | _        d| _        y# |$ r | j                  j                  «        Y yw xY w)Nr   )ÚEmptyéÿÿÿÿFé   T)Útimeout)r   r   Ú_timer   Úacquirer   r   Ú
get_nowaitÚgetÚmaxÚreleaser   r   )r   Úblockingr!   r   ÚstartÚacquiredÚ	remainings          r   r#   z_InterpreterLock.acquire   sø   € ÝØ—
‘
“ˆØ�bŠ=Ø—z‘z×)Ñ)¨(Ó3‰Hà—z‘z×)Ñ)¨(°GÓ<ˆHÙØØ�;Š;Ø�KŠK˜1Ñ�KØð
	ÙØ—‘×&Ñ&Õ(Ø˜B’Ø—‘—‘Õ!ä  7¨d¯j©j«l¸UÑ.BÑ#CÓD�	Ø—‘—‘¨	�Ô2ð —n‘nÓ&ˆŒØˆŒØøð ò 	Ø�J‰J×ÑÔ Ùð	ús   Á:A9D ÄD2Ä1D2c                 ó  — | j                   | j                  «       k7  rt        d«      ‚| xj                  dz  c_        | j                  s"d | _         | j                  j                  d «       | j                  j                  «        y )Nzcannot release un-acquired lockr    )r   r   ÚRuntimeErrorr   r   Úputr   r'   ©r   s    r   r'   z_InterpreterLock.release6   s]   € Ø�;‰;˜$Ÿ.™.Ó*Ò*ÜÐ@ÓAÐAØ�Š�qÑ�Ø�{Š{ØˆDŒKØ�K‰K�O‰O˜DÔ!Ø�
‰
×ÑÕr   c                 ó&   — | j                  «        | S ©N)r#   r/   s    r   Ú	__enter__z_InterpreterLock.__enter__?   s   € Ø�‰ŒØˆr   c                 ó$   — | j                  «        y r1   )r'   )r   Úexcs     r   Ú__exit__z_InterpreterLock.__exit__C   s   € Ø�‰�r   N)Tr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Útimer   r"   r   r#   r'   r2   r5   © r   r   r   r      s$   „ Ù=Ý#Ý'òóò6òór   r   Ú c              #   óÂ   K  — t        | dd«      }|€|xs | j                  «       }t        |||«      }| j                  |«       |–— |€| `y| j                  |«       y­w)z>get (create if necessary) and then restore `tqdm_class`'s lockr   N)ÚgetattrÚget_lockÚset_lockr   )Ú
tqdm_classÚ	lock_nameÚlockÚold_locks       r   Úensure_lockrE   G   si   è ø€ ô �z 7¨DÓ1€HØ€|ØÒ0˜:×.Ñ.Ó0ˆÜ�4˜ DÓ)€DØ×Ñ˜ÔØ
‚JØÐØÑà×Ñ˜HÕ%ùs   ‚AAc           	      ó”   — dt         j                  ›d| j                  ›d| j                  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Úpathr7   r8   ÚsplitÚexec)rA   Úlock_queue_idÚcodes      r   Ú_get_interpreter_initrN   V   sk   € ð	ÜŸ™˜ð %&ð '1×&;Ñ&;Ð%>ð ?Ø!×.Ñ.×4Ñ4°SÓ9Ð<ð =Cð DQÐBSÐSVð		Xð 	ô �$�ˆ=Ðr   c                 ó0   ‡— t        ˆfd„| D «       d¬«      S )z+min(map(length_hint, iterables), default=0)c              3   óF   •K  — | ]  }t        |d «      xŠdk\  sŒ‰–— Œ y­w)r   r   Nr   )Ú.0ÚitÚns     €r   ú	<genexpr>z_min_map_len.<locals>.<genexpr>h   s$   øè ø€ ÒI�b¬{¸2¸rÓ/BÐ*B¨!ÀqÓ)H”ÑIùs   ƒ!š!r   )Údefault)Úmin)Ú	iterablesrS   s    @r   Ú_min_map_lenrX   f   s   ø€ äÓI˜IÔIÐSTÔUÐUr   r    g        )	Úmax_workersr!   Ú	chunksizerB   rA   Ú	smoothingr   Ú_initializerÚ	_initargsc       	         ó  ‡‡— |j                  «       }d|vrt        |«      |d<   i }d|v r|j                  d«      |d<   i }dD ]  }||v sŒ|j                  |«      ||<   Œ d}|d   r6d|vr2	 ddlm} |xs t        d	 |«       xs d
dz   «      }|d   |kD  r||d<   d}t        |||¬«      5 }|	€|j                  }	|f}
 | d||	|
dœ|¤Ž5 } |dd|i|¤Ž5 Š|�d‰_
        |j                  Šˆˆfd„}||_        t         |j                  |g|¢­||dœ|¤Ž«      cddd«       cddd«       cddd«       S # t
        $ r	 ddlm} Y ŒÓw xY w# 1 sw Y   nxY w	 ddd«       n# 1 sw Y   nxY wddd«       y# 1 sw Y   yxY w)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)rB   rC   )rY   ÚinitializerÚinitargsr[   c                  ó@   •—  ‰| i |¤Ž}|j                  ˆfd„«       |S )Nc                 ó$   •— ‰j                  «       S r1   )Úupdate)Ú_Úpbars    €r   ú<lambda>z4_executor_map.<locals>.patchsubmit.<locals>.<lambda>    s   ø€ °D·K±K³M€ r   )Úadd_done_callback)ÚargsÚkwargsÚfutÚ	orisubmitro   s      €€r   Úpatchsubmitz"_executor_map.<locals>.patchsubmitž   s&   ø€ Ù# TÐ4¨VÑ4�CØ×)Ñ)Ó*AÔBØ�Jr   )r!   rZ   r;   )ÚcopyrX   ÚpopÚosre   ÚImportErrorrf   rV   rE   r@   Údynamic_minitersÚsubmitÚlistÚmap)ÚPoolExecutorÚfnrY   r!   rZ   rB   rA   r[   r   r\   r]   rW   Útqdm_kwargsrs   Ú
map_kwargsÚpool_kwargsÚkr{   rf   Ú	rough_maxÚlkÚexrv   ru   ro   s                          @@r   Ú_executor_maprˆ   k   s  ù€ ð$ ×ÑÓ€FØ�fÑÜ& yÓ1ˆˆw‰Ø€JØ�vÑØ#)§:¡:¨lÓ#;ˆ
�<Ñ Ø€KØHò +ˆØ�Š;Ø#ŸZ™Z¨›]ˆK˜ŠNð+ð ÐØˆg‚˜:¨VÑ3ð	%Ý9ð  ÒB¤3 r©I«KÒ,<¸1ÀÑ+AÓ#Bˆ	Ø�'‰?˜YÒ&Ø!*ˆF�:ÑØ#ÐÜ	�Z¨9¸5Ô	Að YÀRàÐØ%×.Ñ.ˆLØ˜ˆIÙð ) k¸|ÐV_ñ )Ø'ñ)ð 	YØ,.ÙÑ: iÐ:°6Ñ:ð Y¸dØ#Ð/Ø,0�DÔ)ØŸI™I�	õð (�”	Ü˜F˜BŸF™FØðXØ"ñXØ,3¸yñXØLVñXó Y÷Yð Y÷	Yð 	Y÷Yñ Yøô ò 	%ß$ð	%ú÷Yð Yúð Y÷	Y÷ 	Yñ 	Yú÷Y÷ Yñ YúsU   Á-D5 Â,E5Ã
E ÃAE
Ä	E Ä"	E5Ä5EÅEÅ
EÅE Å	E5Å E)	Å%E5Å5E>c                 ó.   — ddl m} t        || g|¢­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ˆ   )r€   rW   r�   rŠ   s       r   r
   r
   §   s   € õ. 6ÜÐ+¨RÐK°)ÒK¸{ÑKÐKr   c                 óô   — ddl m} ddlm} |j	                  «       }|j                  d«       |j                  dt        «      }t        ||j                  «      \  }}t        || g|¢­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)ÚInterpreterPoolExecutorNrA   )r   r\   r]   )Ú
concurrentr�   r‹   rŽ   Úcreate_queuer.   r%   Ú	tqdm_autorN   Úidrˆ   r   )	r€   rW   r�   r�   rŽ   Ú
lock_queuerA   ri   rj   s	            r   r   r   Â   s‚   € õ (Ý:Ø×*Ñ*Ó,€JØ‡N�N�4ÔØ—‘ ¬yÓ9€JÜ1°*¸j¿m¹mÓLÑ€K�ÜØ ðEØ&/ñEÜ7GÈ
Ó7SØ ¨HñEà8CñEð Er   Úmp_lock)rB   c                óŽ   — ddl m} |r,d|vr(t        |«      }|dkD  rddlm}  |d|z  t
        d¬«       t        || g|¢­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   )ÚProcessPoolExecutorrZ   iè  )Úwarnz…Iterable length %d > 1000 but `chunksize` is not set. This may seriously degrade multiprocess performance. Set `chunksize=1` or more.r   )Ú
stacklevelrB   )r‹   r–   rX   Úwarningsr—   r   rˆ   )r€   rB   rW   r�   r–   Úshortest_iterable_lenr—   s          r   r   r   Û   sf   € õ: 7Ù�[¨Ñ3ô !-¨YÓ 7ÐØ  4Ò'Ý%Ùð /à1FñGô ¨õ,ô Ð,¨bÐa°9ÒaÈ	ÐaÐU`ÑaÐar   )r<   N)r9   rH   Ú
contextlibr   Úoperatorr   Úautor   r‘   Ústdr   Ú
__author__Ú__all__r   rE   rN   rX   rˆ   r
   r   r   r;   r   r   ú<module>r¡      sŒ   ðñó Ý %Ý  å $Ý à˜k˜]Ð+€
Ú
:€÷5ñ 5ðp ò&ó ð&òò Vð /3¸DÈAÐY[Ø C¨tÀ$ÐRVô9YòxLò6Eð2 +4õ (br   