Your IP : 216.73.216.61


Current Path : /opt/alt/python35/lib/python3.5/site-packages/joblib/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib/python3.5/site-packages/joblib/__pycache__/_parallel_backends.cpython-35.pyc



,|�W�7�@s�dZddlZddlZddlZddlZddlZddlmZmZddl	m
Z
ddlmZm
Z
ddlmZddlmZedk	r�dd	lmZdd
lmZGdd�dee��ZGd
d�de�ZGdd�de�ZGdd�de�ZGdd�dee�ZGdd�deee�ZGdd�de�ZGdd�de�ZGdd�de�Z dS)z,
Backends for embarrassingly parallel code.
�N)�ABCMeta�abstractmethod�)�
format_exc)�WorkerInterrupt�TransportableException)�mp)�with_metaclass)�
MemmapingPool)�
ThreadPoolc@s�eZdZdZedd��Zeddd��Zdddd	�Zd
d�Zdd
�Z	dd�Z
dd�Zddd�ZdS)�ParallelBackendBasezEHelper abc which defines all methods a ParallelBackend must implementcCsdS)a�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. 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.
        N�)�self�n_jobsr
r
�J/opt/alt/python35/lib/python3.5/site-packages/joblib/_parallel_backends.py�effective_n_jobssz$ParallelBackendBase.effective_n_jobsNcCsdS)zSchedule a func to be runNr
)r�func�callbackr
r
r�apply_async*szParallelBackendBase.apply_asyncrcKs||_|j|�S)z�Reconfigure the backend and return the number of workers.

        This makes it possible to reuse an existing backend instance for
        successive independent calls to Parallel with different parameters.
        )�parallelr)rrr�backend_argsr
r
r�	configure.s	zParallelBackendBase.configurecCsdS)z#Shutdown the process or thread poolNr
)rr
r
r�	terminate7szParallelBackendBase.terminatecCsdS)z Determine the optimal batch sizerr
)rr
r
r�compute_batch_size:sz&ParallelBackendBase.compute_batch_sizecCsdS)z1Callback indicate how long it took to run a batchNr
)r�
batch_size�durationr
r
r�batch_completed>sz#ParallelBackendBase.batch_completedcCsgS)z'List of exception types to be captured.r
)rr
r
r�get_exceptionsAsz"ParallelBackendBase.get_exceptionsTcCsdS)a}Abort any running tasks

        This is called when an exception has been raised when executing a tasks
        and all the remaining tasks will be ignored and can therefore be
        aborted to spare computation resources.

        If ensure_ready is True, the backend should be left in an operating
        state as future tasks might be re-submitted via that same backend
        instance.

        If ensure_ready is False, the implementer of this method can decide
        to leave the backend in a closed / terminated state as no new task
        are expected to be submitted to this backend.

        Setting ensure_ready to False is an optimization that can be leveraged
        when aborting tasks via killing processes from a local process pool
        managed by the backend it-self: if we expect no new tasks, there is no
        point in re-creating a new working pool.
        Nr
)r�ensure_readyr
r
r�abort_everythingEsz$ParallelBackendBase.abort_everything)
�__name__�
__module__�__qualname__�__doc__rrrrrrrrrr
r
r
rrs	rc@s1eZdZdZdd�Zddd�ZdS)�SequentialBackendz�A ParallelBackend which will execute all batches sequentially.

    Does not use/create any threading objects, and hence has minimal
    overhead. Used when n_jobs == 1.
    cCs|dkrtd��dS)z?Determine the number of jobs which are going to run in parallelrz&n_jobs == 0 in Parallel has no meaningr)�
ValueError)rrr
r
rresz"SequentialBackend.effective_n_jobsNcCs t|�}|r||�|S)zSchedule a func to be run)�ImmediateResult)rrr�resultr
r
rrks
zSequentialBackend.apply_async)r r!r"r#rrr
r
r
rr$^sr$c@sLeZdZdZdd�Zdd�Zddd�Zd	d
d�ZdS)�PoolManagerMixinz,A helper class for managing pool of workers.cCsd|dkrtd��nEtdks3|dkr7dS|dkr`ttj�d|d�}|S)z?Determine the number of jobs which are going to run in parallelrz&n_jobs == 0 in Parallel has no meaningNr)r%r�max�	cpu_count)rrr
r
rrvsz!PoolManagerMixin.effective_n_jobscCs6|jdk	r2|jj�|jj�d|_dS)z#Shutdown the process or thread poolN)�_pool�closer)rr
r
rr�s

zPoolManagerMixin.terminateNcCs|jjt|�d|�S)zSchedule a func to be runr)r+r�SafeFunction)rrrr
r
rr�szPoolManagerMixin.apply_asyncTcCs<|j�|r8|jd|jjd|j|jj�dS)z@Shutdown the pool and restart a new one with the same parametersrrN)rrrr�
_backend_args)rrr
r
rr�s
z!PoolManagerMixin.abort_everything)r r!r"r#rrrrr
r
r
rr(ss
r(c@sFeZdZdZdZdZdZdZdd�Zdd	�Z	d
S)�AutoBatchingMixinz/A helper class for automagically batching jobs.g�������?�rgcCs	|j}|j}|dkr�||jkr�t||j|�}td|d�}||_|jjdkr�|jjd||f�nb||jkr�|dkr�|d}||_|jjdkr�|jjd||f�n|}||krd|_|S)z Determine the optimal batch sizerr0r�
z:Batch computation too fast (%.4fs.) Setting batch_size=%d.z:Batch computation too slow (%.4fs.) Setting batch_size=%d.)	�_effective_batch_size�_smoothed_batch_duration�MIN_IDEAL_BATCH_DURATION�intr)r�verbose�_print�MAX_IDEAL_BATCH_DURATION)r�old_batch_size�batch_duration�ideal_batch_sizerr
r
rr�s2		

		
			z$AutoBatchingMixin.compute_batch_sizecCsL||jkrH|j}|dkr-|}nd|d|}||_dS)z1Callback indicate how long it took to run a batchrg�������?g�������?N)r2r3)rrr�old_duration�new_durationr
r
rr�s		z!AutoBatchingMixin.batch_completedN)
r r!r"r#r4r8r2r3rrr
r
r
rr/�s.r/c@s(eZdZdZdddd�ZdS)�ThreadingBackenda�A ParallelBackend which will use a thread pool to execute batches in.

    This is a low-overhead backend but it suffers from the Python Global
    Interpreter Lock if the called function relies a lot on Python objects.
    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).
    rNcKsF|j|�}|dkr*tt���||_t|�|_|S)z?Build a process or thread pool and return the number of workersr)r�FallbackToBackendr$rrr+)rrrrr
r
rr�s	zThreadingBackend.configure)r r!r"r#rr
r
r
rr>�sr>csReZdZdZdZ�fdd�Zdddd�Z�fd	d
�Z�S)�MultiprocessingBackendaA ParallelBackend which will use a multiprocessing.Pool.

    Will introduce some communication and memory overhead when exchanging
    input and output data with the with the worker Python processes.
    However, does not suffer from the Python Global Interpreter Lock.
    �__JOBLIB_SPAWNED_PARALLEL__cs�tdkrdStj�jrB|dkr>tjddd�dStj�jdkrntjddd�dStt	|�j
|�S)z�Determine the number of jobs which are going to run in parallel.

        This also checks if we are attempting to create a nested parallel
        loop.
        NrzHMultiprocessing-backed parallel loops cannot be nested, setting n_jobs=1�
stacklevel��
MainThreadzVMultiprocessing backed parallel loops cannot be nested below threads, setting n_jobs=1)r�current_process�daemon�warnings�warn�	threading�current_thread�name�superr@r)rr)�	__class__r
rrsz'MultiprocessingBackend.effective_n_jobsrNcKs�|j|�}|dkr*tt���ttjj|jd��}|rZtd��dtj|j<t	j
�t||�|_||_
|S)z?Build a process or thread pool and return the number of workersrra%[joblib] Attempting to do parallel computing without protecting your import on a system that does not support forking. To use parallel-computing in a script, you must protect your main loop using "if __name__ == '__main__'". Please see the joblib documentation on Parallel for more information�1)rr?r$r5�os�environ�get�JOBLIB_SPAWNED_PROCESS�ImportError�gc�collectr
r+r)rrrr�already_forkedr
r
rr"s	
	z MultiprocessingBackend.configurecs6tt|�j�|jtjkr2tj|j=dS)z#Shutdown the process or thread poolN)rLr@rrRrOrP)r)rMr
rr;sz MultiprocessingBackend.terminate)r r!r"r#rRrrrr
r
)rMrr@�s
r@c@s(eZdZdd�Zdd�ZdS)r&cCs|�|_dS)N)�results)r�batchr
r
r�__init__CszImmediateResult.__init__cCs|jS)N)rW)rr
r
rrQHszImmediateResult.getN)r r!r"rYrQr
r
r
rr&Bsr&c@s.eZdZdZdd�Zdd�ZdS)r-aWrapper that handles the serialization of exception tracebacks.

    If an exception is triggered when calling the inner function, a copy of
    the full traceback is captured to make it possible to serialize
    it so that it can be rendered in a different Python process.
    cCs
||_dS)N)r)rrr
r
rrYSszSafeFunction.__init__cOsy|j||�SWndtk
r1t��YnJtj�\}}}t|||dddd�}t||��YnXdS)N�contextr1�	tb_offsetr)r�KeyboardInterruptr�sys�exc_inforr)r�args�kwargs�e_type�e_value�e_tb�textr
r
r�__call__Vs

zSafeFunction.__call__N)r r!r"r#rYrer
r
r
rr-Lsr-c@s"eZdZdZdd�ZdS)r?z<Raised when configuration should fallback to another backendcCs
||_dS)N)�backend)rrfr
r
rrYgszFallbackToBackend.__init__N)r r!r"r#rYr
r
r
rr?dsr?)!r#rTrOr]rGrI�abcrr�format_stackr�
my_exceptionsrr�_multiprocessing_helpersr�_compatr	�poolr
�multiprocessing.poolrrr$�objectr(r/r>r@r&r-�	Exceptionr?r
r
r
r�<module>s.I"P	G