3
ûâde|  ã               @   s^  d Z ddg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mZmZmZmZ ddlmZ eejej	f Zed	ƒZed
ƒZyddlZW n ek
r¨   dZY nX dZdZeeee dœdd„ƒZdeegef ee eee dœdd„Zdeegef ee eee dœdd„Zdeegef ee eee dœdd„Ze�rRe ZZneZeZdS )ab  Convenient parallelization of higher order functions.

This module provides two helper functions, with appropriate fallbacks on
Python 2 and on systems lacking support for synchronization mechanisms:

- map_multiprocess
- map_multithread

These helpers work like Python 3's map, with two differences:

- They don't guarantee the order of processing of
  the elements of the iterable.
- The underlying process/thread pools chop the iterable into
  a number of chunks, so that for very long iterables using
  a large value for chunksize can make the job complete much faster
  than using the default value of 1.
Úmap_multiprocessÚmap_multithreadé    )Úcontextmanager)ÚPool)Úpool)ÚCallableÚIterableÚIteratorÚTypeVarÚUnion)ÚDEFAULT_POOLSIZEÚSÚTNTFi€„ )r   Úreturnc          
   c   s*   z
| V  W d| j ƒ  | jƒ  | jƒ  X dS )z>Return a context manager making sure the pool closes properly.N)ÚcloseÚjoinÚ	terminate)r   © r   ú;/tmp/pip-build-red6nood/pip/pip/_internal/utils/parallel.pyÚclosing.   s
    
r   é   )ÚfuncÚiterableÚ	chunksizer   c             C   s
   t | |ƒS )zÞMake an iterator applying func to each element in iterable.

    This function is the sequential fallback either on Python 2
    where Pool.imap* doesn't react to KeyboardInterrupt
    or when sem_open is unavailable.
    )Úmap)r   r   r   r   r   r   Ú_map_fallback;   s    	r   c             C   s$   t tƒ ƒ�}|j| ||ƒS Q R X dS )zÿChop iterable into chunks and submit them to a process pool.

    For very long iterables using a large value for chunksize can make
    the job complete much faster than using the default value of 1.

    Return an unordered iterator of the results.
    N)r   ÚProcessPoolÚimap_unordered)r   r   r   r   r   r   r   Ú_map_multiprocessG   s    
r   c             C   s&   t ttƒƒ�}|j| ||ƒS Q R X dS )zþChop iterable into chunks and submit them to a thread pool.

    For very long iterables using a large value for chunksize can make
    the job complete much faster than using the default value of 1.

    Return an unordered iterator of the results.
    N)r   Ú
ThreadPoolr   r   )r   r   r   r   r   r   r   Ú_map_multithreadU   s    
r    )r   )r   )r   )Ú__doc__Ú__all__Ú
contextlibr   Úmultiprocessingr   r   r   Zmultiprocessing.dummyr   Útypingr   r   r	   r
   r   Úpip._vendor.requests.adaptersr   r   r   Zmultiprocessing.synchronizeÚImportErrorZLACK_SEM_OPENÚTIMEOUTr   Úintr   r   r    r   r   r   r   r   r   Ú<module>   s8   

