U
    .d                     @   s  d Z ddlmZ ddlZddlZddlmZ ddlZddlZddl	Z	ddl
Z
ddlmZ ddlmZ ddlZddlZddlmZ dd	lmZ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m Z  ddl!m"Z" ddlm#Z# ddlm$Z$ eedZ%da&dZ'ddhZ(edk	r0ee%d< ddl)m*Z* ee%d< da&dZ+e	, Z-dZ.dZ/dd Z0de0iZ1d4ddZ2G dd de3Z4dZ5e6edrej78dd 9 pdZ:e:dk	rej;e:d!Z5G d"d# d#e3Z<d5d%d&Z=d'd( Z>d)d* Z?G d+d, d,e3Z@d6d-d.ZAd7d0d1ZBG d2d3 d3eZCdS )8z+
Helpers for embarrassingly parallel code.
    )divisionN)sqrt)uuid4)Integral   )mp)Loggershort_format_time)memstr_to_bytes)FallbackToBackendMultiprocessingBackendThreadingBackendSequentialBackendLokyBackend)dumpsloads)	eval_expr)AutoBatchingMixin)ParallelBackendBase)	threading
sequentialr   multiprocessingloky)r   )	processesthreadsN)	sharedmemNc               
   C   sP   zddl m}  td|  W n0 tk
rJ } zd}t||W 5 d}~X Y nX dS )z? Register Dask Backend if called with parallel_backend("dask") r   )DaskDistributedBackenddaskzTo use the dask.distributed backend you must install both the `dask` and distributed modules.

See https://dask.pydata.org/en/latest/install.html for more information.N)Z_daskr   register_parallel_backendImportError)r   emsg r"   3/tmp/pip-unpacked-wheel-ho8k9mhl/joblib/parallel.py_register_daskG   s    r$   r   c           
      C   s  | t krtd| t f |tkr0td|tf | dkrH|dkrHtdttdd}|dk	r|\}}|j}t|dd	}|dkr|stt |d
}|dkrtd|j	j
|j	j
f  |tfS |S tt dd
}t|dd	}t|dd	}	|dkr|r | dkr|	stt dd
}|tfS )z!Return the active default backendz9prefer=%r is not a valid backend hint, expected one of %rz@require=%r is not a valid backend constraint, expected one of %rr   r   zJprefer == 'processes' and require == 'sharedmem' are inconsistent settingsbackend_and_jobsNsupports_sharedmemFnesting_level
   ziUsing %s as joblib.Parallel backend instead of %s as the latter does not provide shared memory semantics.r   uses_threadsr   )VALID_BACKEND_HINTS
ValueErrorVALID_BACKEND_CONSTRAINTSgetattr_backendr(   BACKENDSDEFAULT_THREAD_BACKENDprint	__class____name__DEFAULT_N_JOBSDEFAULT_BACKEND)
preferrequireverboser%   backendn_jobsr(   r&   Zsharedmem_backendr*   r"   r"   r#   get_active_backendY   sJ    r<   c                   @   s2   e Zd ZdZdddZdd Zdd	 Zd
d ZdS )parallel_backendaB  Change the default backend used by Parallel inside a with block.

    If ``backend`` is a string it must match a previously registered
    implementation using the :func:`~register_parallel_backend` function.

    By default the following backends are available:

    - 'loky': single-host, process-based parallelism (used by default),
    - 'threading': single-host, thread-based parallelism,
    - 'multiprocessing': legacy single-host, process-based parallelism.

    'loky' is recommended to run functions that manipulate Python objects.
    'threading' is a low-overhead alternative that is most efficient for
    functions that release the Global Interpreter Lock: e.g. I/O-bound code or
    CPU-bound code in a few calls to native code that explicitly releases the
    GIL. Note that on some rare systems (such as pyiodine),
    multiprocessing and loky may not be available, in which case joblib
    defaults to threading.

    In addition, if the `dask` and `distributed` Python packages are installed,
    it is possible to use the 'dask' backend for better scheduling of nested
    parallel calls without over-subscription and potentially distribute
    parallel calls over a networked cluster of several hosts.

    It is also possible to use the distributed 'ray' backend for distributing
    the workload to a cluster of nodes. To use the 'ray' joblib backend add
    the following lines::

     >>> from ray.util.joblib import register_ray  # doctest: +SKIP
     >>> register_ray()  # doctest: +SKIP
     >>> with parallel_backend("ray"):  # doctest: +SKIP
     ...     print(Parallel()(delayed(neg)(i + 1) for i in range(5)))
     [-1, -2, -3, -4, -5]

    Alternatively the backend can be passed directly as an instance.

    By default all available workers will be used (``n_jobs=-1``) unless the
    caller passes an explicit value for the ``n_jobs`` parameter.

    This is an alternative to passing a ``backend='backend_name'`` argument to
    the :class:`~Parallel` class constructor. It is particularly useful when
    calling into library code that uses joblib internally but does not expose
    the backend argument in its own API.

    >>> from operator import neg
    >>> with parallel_backend('threading'):
    ...     print(Parallel()(delayed(neg)(i + 1) for i in range(5)))
    ...
    [-1, -2, -3, -4, -5]

    Warning: this function is experimental and subject to change in a future
    version of joblib.

    Joblib also tries to limit the oversubscription by limiting the number of
    threads usable in some third-party library threadpools like OpenBLAS, MKL
    or OpenMP. The default limit in each worker is set to
    ``max(cpu_count() // effective_n_jobs, 1)`` but this limit can be
    overwritten with the ``inner_max_num_threads`` argument which will be used
    to set this limit in the child processes.

    .. versionadded:: 0.10

    Nc           	      K   s   t |tr|tkrv|tkr*t| }|  nL|tkr^tjd| dt dtdd tt t|< nt	d|t
t f t| f |}|d k	rd|jj}|jst|||_ttdd }|jd kr|d krd	}n
|d	 j}||_|| _||f| _||ft_d S )
Njoblib backend '3' is not available on your system, falling back to .   
stacklevel'Invalid backend: %s, expected one of %rz>{} does not accept setting the inner_max_num_threads argument.r%   r   )
isinstancestrr0   EXTERNAL_BACKENDSMAYBE_AVAILABLE_BACKENDSwarningswarnr6   UserWarningr,   sortedkeysformatr3   r4   Zsupports_inner_max_num_threadsAssertionErrorinner_max_num_threadsr.   r/   r(   old_backend_and_jobsnew_backend_and_jobsr%   )	selfr:   r;   rQ   Zbackend_paramsregisterr!   Zcurrent_backend_and_jobsr(   r"   r"   r#   __init__   s>    



zparallel_backend.__init__c                 C   s   | j S N)rS   rT   r"   r"   r#   	__enter__   s    zparallel_backend.__enter__c                 C   s   |    d S rW   )
unregister)rT   typevalue	tracebackr"   r"   r#   __exit__   s    zparallel_backend.__exit__c                 C   s,   | j d kr ttdd d k	r(t`n| j t_d S )Nr%   )rR   r.   r/   r%   rX   r"   r"   r#   rZ      s    
zparallel_backend.unregister)r>   N)r4   
__module____qualname____doc__rV   rY   r^   rZ   r"   r"   r"   r#   r=      s
   ?
+r=   get_contextZJOBLIB_START_METHOD )methodc                   @   s2   e Zd ZdZdddZdd Zdd Zd	d
 ZdS )BatchedCallszCWrap a sequence of (func, args, kwargs) tuples as a single callableNc                 C   sX   t || _t| j| _|| _t|tr4|\| _| _n|d  | _| _|d k	rN|ni | _	d S rW   )
listitemslen_size_reducer_callbackrF   tupler/   _n_jobs_pickle_cache)rT   Ziterator_slicer%   Zreducer_callbackZpickle_cacher"   r"   r#   rV     s    

zBatchedCalls.__init__c              
   C   s8   t | j| jd dd | jD W  5 Q R  S Q R X d S )Nr;   c                 S   s   g | ]\}}}|||qS r"   r"   ).0funcargskwargsr"   r"   r#   
<listcomp>   s   z)BatchedCalls.__call__.<locals>.<listcomp>)r=   r/   rl   rg   rX   r"   r"   r#   __call__  s    zBatchedCalls.__call__c                 C   s.   | j d k	r|    t| j| j| jfd | jffS rW   )rj   re   rg   r/   rl   rm   rX   r"   r"   r#   
__reduce__#  s    
zBatchedCalls.__reduce__c                 C   s   | j S rW   )ri   rX   r"   r"   r#   __len__-  s    zBatchedCalls.__len__)NN)r4   r_   r`   ra   rV   rt   ru   rv   r"   r"   r"   r#   re     s     

re   Fc                 C   s   t dkrdS tj| dS )a  Return the number of CPUs.

    This delegates to loky.cpu_count that takes into account additional
    constraints such as Linux CFS scheduler quotas (typically set by container
    runtimes such as docker) and CPU affinity (for instance using the taskset
    command on Linux).

    If only_physical_cores is True, do not take hyperthreading / SMT logical
    cores into account.
    Nr   Zonly_physical_cores)r   r   	cpu_countrw   r"   r"   r#   rx   4  s    rx   c                 C   s\   |sdS |dkrdS | dkr dS dd| d  }t | | }t | d | }t|t|kS )	z Returns False for indices increasingly apart, the distance
        depending on the value of verbose.

        We use a lag increasing as the square of index
    Tr)   Fr   g      ?   rB   r   )r   int)indexr9   ZscaleZ
next_scaler"   r"   r#   _verbosity_filterH  s    r|   c                    s8    fdd}zt  |}W n tk
r2   Y nX |S )z6Decorator used to capture the arguments of a function.c                     s
    | |fS rW   r"   )rq   rr   functionr"   r#   delayed_function^  s    z!delayed.<locals>.delayed_function)	functoolswrapsAttributeError)r~   r   r"   r}   r#   delayed[  s    r   c                   @   s    e Zd ZdZdd Zdd ZdS )BatchCompletionCallBacka_  Callback used by joblib.Parallel's multiprocessing backend.

    This callable is executed by the parent process whenever a worker process
    has returned the results of a batch of tasks.

    It is used for progress reporting, to update estimate of the batch
    processing duration and to schedule the next batch of tasks to be
    processed.

    c                 C   s   || _ || _|| _d S rW   )dispatch_timestamp
batch_sizeparallel)rT   r   r   r   r"   r"   r#   rV   s  s    z BatchCompletionCallBack.__init__c              	   C   sj   | j  j| j7  _t | j }| j j| j| | j   | j j | j j	d k	r\| j 
  W 5 Q R X d S rW   )r   n_completed_tasksr   timer   r/   Zbatch_completedprint_progress_lock_original_iteratordispatch_next)rT   outZthis_batch_durationr"   r"   r#   rt   x  s    

z BatchCompletionCallBack.__call__N)r4   r_   r`   ra   rV   rt   r"   r"   r"   r#   r   h  s   
r   c                 C   s   |t | < |r| adS )a  Register a new Parallel backend factory.

    The new backend can then be selected by passing its name as the backend
    argument to the :class:`~Parallel` class. Moreover, the default backend can
    be overwritten globally by setting make_default=True.

    The factory can be any callable that takes no argument and return an
    instance of ``ParallelBackendBase``.

    Warning: this function is experimental and subject to change in a future
    version of joblib.

    .. versionadded:: 0.10

    N)r0   r6   )namefactoryZmake_defaultr"   r"   r#   r     s    r   r>   c                 C   s"   t  \}}| dkr|} |j| dS )af  Determine the number of jobs that can actually run in parallel

    n_jobs is the number of workers requested by the callers. Passing n_jobs=-1
    means requesting all available workers for instance matching the number of
    CPU cores on the worker host(s).

    This method should return a guesstimate of the number of workers that can
    actually perform work concurrently with the currently enabled default
    backend. The primary use case is to make it possible for the caller to know
    in how many chunks to slice the work.

    In general working on larger data chunks is more efficient (less scheduling
    overhead and better use of CPU cache prefetching heuristics) as long as all
    the workers have enough work to do.

    Warning: this function is experimental and subject to change in a future
    version of joblib.

    .. versionadded:: 0.10

    Nrn   )r<   effective_n_jobs)r;   r:   Zbackend_n_jobsr"   r"   r#   r     s    
r   c                   @   s   e Zd ZdZd$dd	Zd
d Zdd Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zd d! Zd"d# ZdS )%ParallelaC-   Helper class for readable parallel mapping.

        Read more in the :ref:`User Guide <parallel>`.

        Parameters
        -----------
        n_jobs: int, default: None
            The maximum number of concurrently running jobs, such as the number
            of Python worker processes when backend="multiprocessing"
            or the size of the thread-pool when backend="threading".
            If -1 all CPUs are used. If 1 is given, no parallel computing code
            is used at all, which is useful for debugging. For n_jobs below -1,
            (n_cpus + 1 + n_jobs) are used. Thus for n_jobs = -2, all
            CPUs but one are used.
            None is a marker for 'unset' that will be interpreted as n_jobs=1
            (sequential execution) unless the call is performed under a
            :func:`~parallel_backend` context manager that sets another value
            for n_jobs.
        backend: str, ParallelBackendBase instance or None, default: 'loky'
            Specify the parallelization backend implementation.
            Supported backends are:

            - "loky" used by default, can induce some
              communication and memory overhead when exchanging input and
              output data with the worker Python processes. On some rare
              systems (such as Pyiodide), the loky backend may not be
              available.
            - "multiprocessing" previous process-based backend based on
              `multiprocessing.Pool`. Less robust than `loky`.
            - "threading" is a very low-overhead backend but it suffers
              from the Python Global Interpreter Lock if the called function
              relies a lot on Python objects. "threading" is mostly useful
              when the execution bottleneck is a compiled extension that
              explicitly releases the GIL (for instance a Cython loop wrapped
              in a "with nogil" block or an expensive call to a library such
              as NumPy).
            - finally, you can register backends by calling
              :func:`~register_parallel_backend`. This will allow you to
              implement a backend of your liking.

            It is not recommended to hard-code the backend name in a call to
            :class:`~Parallel` in a library. Instead it is recommended to set
            soft hints (prefer) or hard constraints (require) so as to make it
            possible for library users to change the backend from the outside
            using the :func:`~parallel_backend` context manager.
        prefer: str in {'processes', 'threads'} or None, default: None
            Soft hint to choose the default backend if no specific backend
            was selected with the :func:`~parallel_backend` context manager.
            The default process-based backend is 'loky' and the default
            thread-based backend is 'threading'. Ignored if the ``backend``
            parameter is specified.
        require: 'sharedmem' or None, default None
            Hard constraint to select the backend. If set to 'sharedmem',
            the selected backend will be single-host and thread-based even
            if the user asked for a non-thread based backend with
            parallel_backend.
        verbose: int, optional
            The verbosity level: if non zero, progress messages are
            printed. Above 50, the output is sent to stdout.
            The frequency of the messages increases with the verbosity level.
            If it more than 10, all iterations are reported.
        timeout: float, optional
            Timeout limit for each task to complete.  If any task takes longer
            a TimeOutError will be raised. Only applied when n_jobs != 1
        pre_dispatch: {'all', integer, or expression, as in '3*n_jobs'}
            The number of batches (of tasks) to be pre-dispatched.
            Default is '2*n_jobs'. When batch_size="auto" this is reasonable
            default and the workers should never starve. Note that only basic
            arithmetics are allowed here and no modules can be used in this
            expression.
        batch_size: int or 'auto', default: 'auto'
            The number of atomic tasks to dispatch at once to each
            worker. When individual evaluations are very fast, dispatching
            calls to workers can be slower than sequential computation because
            of the overhead. Batching fast computations together can mitigate
            this.
            The ``'auto'`` strategy keeps track of the time it takes for a batch
            to complete, and dynamically adjusts the batch size to keep the time
            on the order of half a second, using a heuristic. The initial batch
            size is 1.
            ``batch_size="auto"`` with ``backend="threading"`` will dispatch
            batches of a single task at a time as the threading backend has
            very little overhead and using larger batch size has not proved to
            bring any gain in that case.
        temp_folder: str, optional
            Folder to be used by the pool for memmapping large arrays
            for sharing memory with worker processes. If None, this will try in
            order:

            - a folder pointed by the JOBLIB_TEMP_FOLDER environment
              variable,
            - /dev/shm if the folder exists and is writable: this is a
              RAM disk filesystem available by default on modern Linux
              distributions,
            - the default system temporary folder that can be
              overridden with TMP, TMPDIR or TEMP environment
              variables, typically /tmp under Unix operating systems.

            Only active when backend="loky" or "multiprocessing".
        max_nbytes int, str, or None, optional, 1M by default
            Threshold on the size of arrays passed to the workers that
            triggers automated memory mapping in temp_folder. Can be an int
            in Bytes, or a human-readable string, e.g., '1M' for 1 megabyte.
            Use None to disable memmapping of large arrays.
            Only active when backend="loky" or "multiprocessing".
        mmap_mode: {None, 'r+', 'r', 'w+', 'c'}, default: 'r'
            Memmapping mode for numpy arrays passed to workers. None will
            disable memmapping, other modes defined in the numpy.memmap doc:
            https://numpy.org/doc/stable/reference/generated/numpy.memmap.html
            Also, see 'max_nbytes' parameter documentation for more details.

        Notes
        -----

        This object uses workers to compute in parallel the application of a
        function to many different arguments. The main functionality it brings
        in addition to using the raw multiprocessing or concurrent.futures API
        are (see examples for details):

        * More readable code, in particular since it avoids
          constructing list of arguments.

        * Easier debugging:
            - informative tracebacks even when the error happens on
              the client side
            - using 'n_jobs=1' enables to turn off parallel computing
              for debugging without changing the codepath
            - early capture of pickling errors

        * An optional progress meter.

        * Interruption of multiprocesses jobs with 'Ctrl-C'

        * Flexible pickling control for the communication to and from
          the worker processes.

        * Ability to use shared memory efficiently with worker
          processes for large numpy-based datastructures.

        Examples
        --------

        A simple example:

        >>> from math import sqrt
        >>> from joblib import Parallel, delayed
        >>> Parallel(n_jobs=1)(delayed(sqrt)(i**2) for i in range(10))
        [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]

        Reshaping the output when the function has several return
        values:

        >>> from math import modf
        >>> from joblib import Parallel, delayed
        >>> r = Parallel(n_jobs=1)(delayed(modf)(i/2.) for i in range(10))
        >>> res, i = zip(*r)
        >>> res
        (0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5)
        >>> i
        (0.0, 0.0, 1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0)

        The progress meter: the higher the value of `verbose`, the more
        messages:

        >>> from time import sleep
        >>> from joblib import Parallel, delayed
        >>> r = Parallel(n_jobs=2, verbose=10)(delayed(sleep)(.2) for _ in range(10)) #doctest: +SKIP
        [Parallel(n_jobs=2)]: Done   1 tasks      | elapsed:    0.6s
        [Parallel(n_jobs=2)]: Done   4 tasks      | elapsed:    0.8s
        [Parallel(n_jobs=2)]: Done  10 out of  10 | elapsed:    1.4s finished

        Traceback example, note how the line of the error is indicated
        as well as the values of the parameter passed to the function that
        triggered the exception, even though the traceback happens in the
        child process:

        >>> from heapq import nlargest
        >>> from joblib import Parallel, delayed
        >>> Parallel(n_jobs=2)(delayed(nlargest)(2, n) for n in (range(4), 'abcde', 3)) #doctest: +SKIP
        #...
        ---------------------------------------------------------------------------
        Sub-process traceback:
        ---------------------------------------------------------------------------
        TypeError                                          Mon Nov 12 11:37:46 2012
        PID: 12934                                    Python 2.7.3: /usr/bin/python
        ...........................................................................
        /usr/lib/python2.7/heapq.pyc in nlargest(n=2, iterable=3, key=None)
            419         if n >= size:
            420             return sorted(iterable, key=key, reverse=True)[:n]
            421
            422     # When key is none, use simpler decoration
            423     if key is None:
        --> 424         it = izip(iterable, count(0,-1))                    # decorate
            425         result = _nlargest(n, it)
            426         return map(itemgetter(0), result)                   # undecorate
            427
            428     # General case, slowest method
         TypeError: izip argument #1 must support iteration
        ___________________________________________________________________________


        Using pre_dispatch in a producer/consumer situation, where the
        data is generated on the fly. Note how the producer is first
        called 3 times before the parallel loop is initiated, and then
        called to generate new data on the fly:

        >>> from math import sqrt
        >>> from joblib import Parallel, delayed
        >>> def producer():
        ...     for i in range(6):
        ...         print('Produced %s' % i)
        ...         yield i
        >>> out = Parallel(n_jobs=2, verbose=100, pre_dispatch='1.5*n_jobs')(
        ...                delayed(sqrt)(i) for i in producer()) #doctest: +SKIP
        Produced 0
        Produced 1
        Produced 2
        [Parallel(n_jobs=2)]: Done 1 jobs     | elapsed:  0.0s
        Produced 3
        [Parallel(n_jobs=2)]: Done 2 jobs     | elapsed:  0.0s
        Produced 4
        [Parallel(n_jobs=2)]: Done 3 jobs     | elapsed:  0.0s
        Produced 5
        [Parallel(n_jobs=2)]: Done 4 jobs     | elapsed:  0.0s
        [Parallel(n_jobs=2)]: Done 6 out of 6 | elapsed:  0.0s remaining: 0.0s
        [Parallel(n_jobs=2)]: Done 6 out of 6 | elapsed:  0.0s finished

    Nr   
2 * n_jobsauto1Mrc              
   C   sF  t |
||d\}}|j}|d kr,|d kr,|}|d kr8d}|| _|| _|| _|| _t | _t	 j
| _d | _t|tr|t|}t||	||
|td| jd d| _td k	rt| jd< nttdrt | jd< |d kr|}nt|tr|jd kr||_nt|dr"t|d	r"|| jd< t|d
}n|tkrp|tkrptjd| dt dtdd tt t|< tt |d
}nVzt| }W n> tk
r } zt d|t!t" f |W 5 d }~X Y nX ||d
}|dkrt#|ddst d| |dks
t|t$r|dkr|| _%nt d| || _&d | _'t( | _)d| _*t+, | _-d S )N)r7   r8   r9   r   r   2   )
max_nbytes	mmap_modetemp_folderr7   r8   r9   contextrb   ZPoolLockr'   r?   r@   rA   rB   rC   rE   r   r&   Fz)Backend %s does not support shared memoryr   z8batch_size must be 'auto' or a positive integer, got: %r).r<   r(   r;   r9   timeoutpre_dispatchqueueQueue_ready_batchesr   hex_idrj   rF   rG   r
   dictmax_backend_argsDEFAULT_MP_CONTEXThasattrr   rb   r   r   r0   rI   rJ   rK   r6   rL   KeyErrorr,   rM   rN   r.   r   r   r/   _outputrf   _jobs_managed_backendr   RLockr   )rT   r;   r:   r9   r   r   r   r   r   r   r7   r8   Zactive_backendZcontext_n_jobsr(   Zbackend_factoryr    r"   r"   r#   rV     s      










zParallel.__init__c                 C   s   d| _ |   | S )NT)r   _initialize_backendrX   r"   r"   r#   rY     s    zParallel.__enter__c                 C   s   |    d| _d S )NF)_terminate_backendr   )rT   exc_type	exc_valuer]   r"   r"   r#   r^      s    zParallel.__exit__c              
   C   s   zL| j jf | j| d| j}| jdk	rJ| j jsJtd| j j	j
| j W n2 tk
r~ } z|j| _ |  }W 5 d}~X Y nX |S )z?Build a process or thread pool and return the number of workers)r;   r   NzThe backend class {!r} does not support timeout. You have set 'timeout={}' in Parallel but the 'timeout' parameter will not be used.)r/   	configurer;   r   r   supports_timeoutrJ   rK   rO   r3   r4   r   r:   r   )rT   r;   r    r"   r"   r#   r     s    zParallel._initialize_backendc                 C   s   | j r| j | jS dS )Nr   )r/   r   r;   rX   r"   r"   r#   _effective_n_jobs  s    zParallel._effective_n_jobsc                 C   s   | j d k	r| j   d S rW   )r/   	terminaterX   r"   r"   r#   r     s    
zParallel._terminate_backendc              	   C   s   | j r
dS |  jt|7  _|  jd7  _t }t|t|| }| j. t| j}| jj	||d}| j
|| W 5 Q R X dS )zQueue the batch for computing, with or without multiprocessing

        WARNING: this method is not thread-safe: it should be only called
        indirectly via dispatch_one_batch.

        Nr   )callback)	_abortingn_dispatched_tasksrh   n_dispatched_batchesr   r   r   r   r/   Zapply_asyncinsert)rT   batchr   cbZjob_idxjobr"   r"   r#   	_dispatch!  s    
zParallel._dispatchc                 C   s   |  | jsd| _d| _dS )a  Dispatch more data for parallel processing

        This method is meant to be called concurrently by the multiprocessing
        callback. We rely on the thread-safety of dispatch_one_batch to protect
        against concurrent consumption of the unprotected iterator.

        FN)dispatch_one_batchr   
_iteratingrX   r"   r"   r#   r   :  s    zParallel.dispatch_nextc           	   
   C   sb  | j dkr| j }n| j }| j4 z| jjdd}W n tjk
r   | j}|| }t	t
||}t|dkrY W 5 Q R  dS || jkrt||k rtdt|d|  }ntdt|| }tdt||D ]4}t||||  | j | j| j}| j| q| jjdd}Y nX t|dkr<W 5 Q R  dS | | W 5 Q R  dS W 5 Q R X dS )	aT  Prefetch the tasks for the next batch and dispatch them.

        The effective size of the batch is computed here.
        If there are no more jobs to dispatch, return False, else return True.

        The iterator consumption and dispatching is protected by the same
        lock so calling this function should be thread safe.

        r   F)blockr   r   r)   TN)r   r/   Zcompute_batch_sizer   r   getr   Empty_cached_effective_n_jobsrf   	itertoolsislicerh   r   r   rangere   Zget_nested_backendrj   rm   putr   )	rT   iteratorr   Ztasksr;   Zbig_batch_sizer   Zfinal_batch_sizeir"   r"   r#   r   F  s:    





zParallel.dispatch_one_batchc                 C   sB   | j s
dS | j dk rtjj}ntjj}|| }|d| |f  dS )z=Display the message on stout or stderr depending on verbosityNr   z	[%s]: %s
)r9   sysstderrwritestdout)rT   r!   Zmsg_argswriterr"   r"   r#   _print  s    

zParallel._printc                 C   s   | j s
dS t | j }| jdk	rLt| j| j r4dS | d| jt|f n| j}| j	}|dks|| d | j
 }|| j  d }|d |k}|s|| rdS || | j	|d   }| d||t|t|f dS )zvDisplay the process of the parallel execution only a fraction
           of time, controlled by self.verbose.
        Nz!Done %3i tasks      | elapsed: %sr   r   g      ?z/Done %3i out of %3i | elapsed: %s remaining: %s)r9   r   _start_timer   r|   r   r   r   r	   r   _pre_dispatch_amount)rT   elapsed_timer{   Ztotal_taskscursorZ	frequencyZis_last_itemZremaining_timer"   r"   r#   r     s>    

zParallel.print_progressc              
   C   s   t  | _| jst| jdkrt| jdkr6td q| j | jd}W 5 Q R X z:t	| j
ddr|| j|j| jd n| j|  W q tk
r } z6d| _| j
}|d k	rt|dr| j}|j|d  W 5 d }~X Y qX qd S )	Nr   g{Gz?r   F)r   Tabort_everything)ensure_ready)rf   r   r   rh   r   r   sleepr   popr.   r/   extendr   r   BaseExceptionr   r   r   r   )rT   r   	exceptionr:   r   r"   r"   r#   retrieve  s(    
zParallel.retrievec           	   	      s   j rtdd _ js$  }n  }t jtrJ fdd}| _	| _
 jjj}|dkrntd|  d||f t jdr j  t|} j}|d	ks|d
krd  _d _n@| _t|drt|dt|}t|  _}t| j}t  _d _d _d _t   _!zd _% &|rD jd k	 _% &|rTqD|d	ksh|d
krnd _% j'   (  W 5 Q R X t  j } dt) j*t) j*t+|f W 5 t jdr؈ j"   js #  t$  _ d  _!X  j*}d  _*|S )Nz)This Parallel instance is already runningFc                      s    j jj j d S rW   )r/   Z_workersZ_temp_folder_managerZset_current_contextr   r"   rX   r"   r#   _batched_calls_reducer_callback  s    
z:Parallel.__call__.<locals>._batched_calls_reducer_callbackr   z%s has no active worker.z,Using backend %s with %d concurrent workers.
start_callallr   endswithr;   	stop_callz*Done %3i out of %3i | elapsed: %s finished),r   r,   r   r   r   r   rF   r/   r   rj   r   r3   r4   RuntimeErrorr   r   r   iterr   r   r   r   replacerG   rz   r   r   r   r   r   r   r   r   rm   r   r   rf   r   r   Zretrieval_contextr   rh   r   r	   )	rT   iterabler;   r   backend_namer   r   r   outputr"   rX   r#   rt     sx    





zParallel.__call__c                 C   s   d| j j| jf S )Nz%s(n_jobs=%s))r3   r4   r;   rX   r"   r"   r#   __repr__[  s    zParallel.__repr__)NNr   Nr   r   Nr   r   NN)r4   r_   r`   ra   rV   rY   r^   r   r   r   r   r   r   r   r   r   rt   r   r"   r"   r"   r#   r     s0    e            
^B+(sr   )NNr   )F)F)r>   )Dra   
__future__r   osr   mathr   r   r   r   r   uuidr   Znumbersr   rJ   r   Z_multiprocessing_helpersr   loggerr   r	   Zdiskr
   Z_parallel_backendsr   r   r   r   r   Zexternals.cloudpickler   r   _utilsr   r   r   r0   r6   r5   rI   Z	externalsr   r1   localr/   r+   r-   r$   rH   r<   objectr=   r   r   environr   striprd   rb   re   rx   r|   r   r   r   r   r   r"   r"   r"   r#   <module>   sl   
 
.~
(


