
вEbc           @   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 d d l m Z d d	 l m Z e r d d
 l m Z d d l m Z m Z m Z m Z m Z e e j e j f Z e d  Z e d  Z n  y d d l Z Wn e k
 re Z n Xe Z d Z e d    Z  d d  Z! d d  Z" d d  Z# e sfe rse! Z$ Z% n e" Z$ e# Z% d S(   sb  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.
t   map_multiprocesst   map_multithreadi(   t   contextmanager(   t   Pool(   t   DEFAULT_POOLSIZE(   t   PY2(   t   map(   t   MYPY_CHECK_RUNNING(   t   pool(   t   Callablet   Iterablet   Iteratort   TypeVart   Uniont   St   TNi c         c   s/   z	 |  VWd |  j    |  j   |  j   Xd S(   s>   Return a context manager making sure the pool closes properly.N(   t   closet   joint	   terminate(   R   (    (    s9   /tmp/pip-build-UPPWic/pip/pip/_internal/utils/parallel.pyt   closing4   s
    	

i   c         C   s   t  |  |  S(   s   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.
    (   R   (   t   funct   iterablet	   chunksize(    (    s9   /tmp/pip-build-UPPWic/pip/pip/_internal/utils/parallel.pyt   _map_fallbackB   s    c         C   s/   t  t     } | j |  | |  SWd QXd S(   s   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   t   ProcessPoolt   imap_unordered(   R   R   R   R   (    (    s9   /tmp/pip-build-UPPWic/pip/pip/_internal/utils/parallel.pyt   _map_multiprocessM   s    	c         C   s2   t  t t    } | j |  | |  SWd QXd S(   s   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   t
   ThreadPoolR   R   (   R   R   R   R   (    (    s9   /tmp/pip-build-UPPWic/pip/pip/_internal/utils/parallel.pyt   _map_multithreadZ   s    	(&   t   __doc__t   __all__t
   contextlibR   t   multiprocessingR   R   t   multiprocessing.dummyR   t   pip._vendor.requests.adaptersR   t   pip._vendor.sixR   t   pip._vendor.six.movesR   t   pip._internal.utils.typingR   R   t   typingR	   R
   R   R   R   R   R   t   multiprocessing.synchronizet   ImportErrort   Truet   LACK_SEM_OPENt   Falset   TIMEOUTR   R   R   R   R    R   (    (    (    s9   /tmp/pip-build-UPPWic/pip/pip/_internal/utils/parallel.pyt   <module>   s8   (
