
    o>qjy&              
           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      V/opt/rentech/trading_bot/.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   )EmptyF   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=zz))(3Hzz))(G<H;;KK1K
	&&(B!7djjlU.B#CD		2 nn&  	JJ 	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{{DKKKOOD!

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      z7D1H|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4S9< =C DQBSSV		X 	 $=r   c                 ,    t        fd| D              S )z min(map(length_hint, iterables))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$     HRk"b.A)Aa(GqHs   !!)min)	iterablesrS   s    @r   _min_map_lenrW   f   s    H9HHHr   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   )rX   initializerinitargsrZ   c                  @     | i |}|j                  fd       |S )Nc                 $    j                         S r1   )update)_pbars    r   <lambda>z4_executor_map.<locals>.patchsubmit.<locals>.<lambda>   s    DKKM r   )add_done_callback)argskwargsfut	orisubmitrn   s      r   patchsubmitz"_executor_map.<locals>.patchsubmit   s&    #T4V4C))*ABJr   )r!   rY   r;   )copyrW   poposrd   ImportErrorre   rU   rE   r@   dynamic_miniterssubmitlistmap)PoolExecutorfnrX   r!   rY   rB   rA   rZ   r   r[   r\   rV   tqdm_kwargsrr   
map_kwargspool_kwargskrz   re   	rough_maxlkexru   rt   rn   s                          @@r   _executor_mapr   k   s   $ Ff&y1wJv#)::l#;
< KH +;#ZZ]KN+ g:V3	%9  B3rIK,<1+A#B	'?Y&!*F:#	Z95	A YR%..LI )k|V_ )') 	Y,.:i:6: Yd#/,0D)II	 (	FBFFX"X,3y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   rV   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   rV   r   r   r   
lock_queuerA   rh   ri   s	            r   r   r      s     (:**,JNN4y9J1*jmm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   )ProcessPoolExecutorrY   i  )warnzIterable length %d > 1000 but `chunksize` is not set. This may seriously degrade multiprocess performance. Set `chunksize=1` or more.r   )
stacklevelrB   )r   r   rW   warningsr   r   r   )r   rB   rV   r   r   shortest_iterable_lenr   s          r   r   r      sf    : 7[3 !-Y 7 4'% /1FG , ,ba9a	aU`aar   )r<   N)r9   rH   
contextlibr   operatorr   autor   r   stdr   
__author____all__r   rE   rN   rW   r   r
   r   r   r;   r   r   <module>r      s     %   $ k]+

:5 5p & & I /3DAY[Ct$RV9YxL6E2 +4 (br   