3
d$                 @   s   d Z ddlmZ ddlmZ ddlmZ ddlm	Z	 yddl
mZ W n ek
rb   ddd	ZY nX ydd
lmZ W nB ek
r   ydd
lmZ W n ek
r   dd ZY nX Y nX ddl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`.
    )absolute_import)contextmanager   )tqdm)TqdmWarning)length_hintc             C   s"   yt | S  tk
r   |S X dS )z,Returns `len(it)`, falling back to `default`N)len	TypeError)itdefault r   7/tmp/pip-build-v9q4h5k9/tqdm/tqdm/contrib/concurrent.pyr      s    r   )	cpu_countc               C   s   dS )N   r   r   r   r   r   r      s    r   Nzgithub.com/Z	casperdcl
thread_mapprocess_map c             c   sP   t | dd}|p| j }t |||}| j| |V  |dkrB| `n
| j| 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lockr   r   r   ensure_lock"   s    
r   c             O   s   |j  }d|kr t|d |d< |jdt}|jdtdt d }|jdd}|jd	d
}t||d}	d|i}
tjdd }|dkr|
j	|j
|	fd i }d|  k odk n  s|j	|d | f |
 }t||j|f||f|S Q R X W d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    r   	chunksize   r   r   )r   Nr         )ZinitializerZinitargs   )r   )r   r    )r   r   )r   r!   )copyr   pop	tqdm_autominr   r   sysversion_infoupdater   listmap)ZPoolExecutorfn	iterablestqdm_kwargskwargsr   r   r   r   Zlkpool_kwargssys_versionZmap_argsexr   r   r   _executor_map0   s"    r2   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.futuresr3   r2   )r+   r,   r-   r3   r   r   r   r   O   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|j }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)
r4   r5   maxr*   r   warningsr6   r   r"   r2   )r+   r,   r-   r5   Zlongest_iterable_lenr6   r   r   r   r   a   s    
)r   )r   )__doc__
__future__r   
contextlibr   autor   r$   Zstdr   operatorr   ImportErrorosr   multiprocessingr&   
__author____all__r   r2   r   r   r   r   r   r   <module>   s.   
