U
    a+d                     @   s|   d Z ddlm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d
dgZedddZdd Zdd
 Zdd ZdS )z,
Thin wrappers around `concurrent.futures`.
    )contextmanager)length_hint)	cpu_count   )tqdm)TqdmWarningzgithub.com/Z	casperdcl
thread_mapprocess_map c                 c   sP   t | dd}|p|  }t |||}| | |V  |dkrB| `n
| | dS )z>get (create if necessary) and then restore `tqdm_class`'s lock_lockN)getattrZget_lockset_lockr   )
tqdm_class	lock_nameZold_locklock r   ;/tmp/pip-unpacked-wheel-tf4cttdx/tqdm/contrib/concurrent.pyensure_lock   s    
r   c                 O   s   |  }d|kr t|d |d< |dt}|dtdt d }|dd}|d	d
}t||dZ}	| ||j|	fd<}
t||
j	|f|d|if|W  5 Q R  W  5 Q R  S Q R X W 5 Q R X dS )a  
    Implementation of `thread_map` and `process_map`.

    Parameters
    ----------
    tqdm_class  : [default: tqdm.auto.tqdm].
    max_workers  : [default: min(32, cpu_count() + 4)].
    chunksize  : [default: 1].
    lock_name  : [default: "":str].
    totalr   r   max_workers       	chunksize   r   r
   )r   )r   ZinitializerZinitargsN)
copyr   pop	tqdm_autominr   r   r   listmap)ZPoolExecutorfn	iterablestqdm_kwargskwargsr   r   r   r   Zlkexr   r   r   _executor_map   s    r%   c                 O   s   ddl m} t|| f||S )a  
    Equivalent of `list(map(fn, *iterables))`
    driven by `concurrent.futures.ThreadPoolExecutor`.

    Parameters
    ----------
    tqdm_class  : optional
        `tqdm` class to use for bars [default: tqdm.auto.tqdm].
    max_workers  : int, optional
        Maximum number of workers to spawn; passed to
        `concurrent.futures.ThreadPoolExecutor.__init__`.
        [default: max(32, cpu_count() + 4)].
    r   )ThreadPoolExecutor)concurrent.futuresr&   r%   )r    r!   r"   r&   r   r   r   r   6   s    c                 O   sv   ddl m} |rLd|krLttt|}|dkrLddlm} |d| tdd d	|krd| }d
|d	< t	|| f||S )a  
    Equivalent of `list(map(fn, *iterables))`
    driven by `concurrent.futures.ProcessPoolExecutor`.

    Parameters
    ----------
    tqdm_class  : optional
        `tqdm` class to use for bars [default: tqdm.auto.tqdm].
    max_workers  : int, optional
        Maximum number of workers to spawn; passed to
        `concurrent.futures.ProcessPoolExecutor.__init__`.
        [default: min(32, cpu_count() + 4)].
    chunksize  : int, optional
        Size of chunks sent to worker processes; passed to
        `concurrent.futures.ProcessPoolExecutor.map`. [default: 1].
    lock_name  : str, optional
        Member of `tqdm_class.get_lock()` to use [default: mp_lock].
    r   )ProcessPoolExecutorr   i  )warnzIterable length %d > 1000 but `chunksize` is not set. This may seriously degrade multiprocess performance. Set `chunksize=1` or more.r   )
stacklevelr   Zmp_lock)
r'   r(   maxr   r   warningsr)   r   r   r%   )r    r!   r"   r(   Zlongest_iterable_lenr)   r   r   r   r	   H   s     N)r
   )__doc__
contextlibr   operatorr   osr   autor   r   Zstdr   
__author____all__r   r%   r   r	   r   r   r   r   <module>   s   
