U
    .eb                     @   s   d Z ddlZddlZddlZddlZddlmZ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 ddlmZ dd	lmZ dd
lmZ dZedZdddddejdejfddZG dd dZdS )zBase Execution Pool.    N)AnyDict)ExceptionInfo)WorkerLostError)	safe_repr)WorkerShutdownWorkerTerminatereraise)timer2)
get_logger)truncate)BasePoolapply_targetzcelery.pool c	                 K   s   |si n|}|r"||p| |  z| ||}
W n |k
rF    Y n t k
rZ    Y n ttfk
rr    Y nj tk
r } zDz tttt|t d  W n tk
r   |t	  Y nX W 5 d}~X Y n
X ||
 dS )z#Apply function within pool context.   N)
	Exceptionr   r   BaseExceptionr	   r   reprsysexc_infor   )targetargskwargscallbackZaccept_callbackpidgetpid	propagate	monotonic_retexcr   r   ;/tmp/pip-unpacked-wheel-f4liivr4/celery/concurrency/base.pyr      s&    
"r   c                   @   s  e Zd ZdZdZdZdZejZdZ	dZ
dZdZdZdZdZdZd8d	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d9dd Zd!d" Zd#d$ Zd%d& Zd'd( Z d)d* Z!d+d, Z"d:d-d.Z#e$e%e&f d/d0d1Z'e(d2d3 Z)e(d4d5 Z*e(d6d7 Z+dS );r   z
Task pool.   r      TFNr   c                 K   s(   || _ || _|| _|| _|| _|| _d S N)limitputlocksoptionsforking_enablecallbacks_propagateapp)selfr%   r&   r(   r)   r*   r'   r   r   r!   __init__I   s    zBasePool.__init__c                 C   s   d S r$   r   r+   r   r   r!   on_startR   s    zBasePool.on_startc                 C   s   dS )NTr   r-   r   r   r!   did_start_okU   s    zBasePool.did_start_okc                 C   s   d S r$   r   r-   r   r   r!   flushX   s    zBasePool.flushc                 C   s   d S r$   r   r-   r   r   r!   on_stop[   s    zBasePool.on_stopc                 C   s   d S r$   r   )r+   Zloopr   r   r!   register_with_event_loop^   s    z!BasePool.register_with_event_loopc                 O   s   d S r$   r   r+   r   r   r   r   r!   on_applya   s    zBasePool.on_applyc                 C   s   d S r$   r   r-   r   r   r!   on_terminated   s    zBasePool.on_terminatec                 C   s   d S r$   r   r+   Zjobr   r   r!   on_soft_timeoutg   s    zBasePool.on_soft_timeoutc                 C   s   d S r$   r   r6   r   r   r!   on_hard_timeoutj   s    zBasePool.on_hard_timeoutc                 O   s   d S r$   r   r3   r   r   r!   maintain_poolm   s    zBasePool.maintain_poolc                 C   s   t t|  dd S )Nz does not implement kill_jobNotImplementedErrortype)r+   r   signalr   r   r!   terminate_jobp   s    zBasePool.terminate_jobc                 C   s   t t|  dd S )Nz does not implement restartr:   r-   r   r   r!   restartt   s    zBasePool.restartc                 C   s   |    | j| _d S r$   )r1   	TERMINATE_stater-   r   r   r!   stopx   s    zBasePool.stopc                 C   s   | j | _|   d S r$   )r@   rA   r5   r-   r   r   r!   	terminate|   s    zBasePool.terminatec                 C   s"   t tj| _|   | j| _d S r$   )loggerisEnabledForloggingDEBUG_does_debugr.   RUNrA   r-   r   r   r!   start   s    zBasePool.startc                 C   s   | j | _|   d S r$   )CLOSErA   on_closer-   r   r   r!   close   s    zBasePool.closec                 C   s   d S r$   r   r-   r   r   r!   rL      s    zBasePool.on_closec                 K   sb   |si n|}|sg n|}| j rBtd|tt|dtt|d | j|||f| j| jd|S )zEquivalent of the :func:`apply` built-in function.

        Callbacks should optimally return as soon as possible since
        otherwise the thread which handles the result will get blocked.
        z&TaskPool: Apply %s (args:%s kwargs:%s)i   )Zwaitforslotr)   )rH   rD   debugr   r   r4   r&   r)   )r+   r   r   r   r'   r   r   r!   apply_async   s     zBasePool.apply_async)returnc                 C   s   | j jd | j j | jdS )z
        Return configuration and statistics information. Subclasses should
        augment the data as required.

        :return: The returned value must be JSON-friendly.
        :)implementationzmax-concurrency)	__class__
__module____name__r%   r-   r   r   r!   	_get_info   s    zBasePool._get_infoc                 C   s   |   S r$   )rV   r-   r   r   r!   info   s    zBasePool.infoc                 C   s   | j | jkS r$   )rA   rI   r-   r   r   r!   active   s    zBasePool.activec                 C   s   | j S r$   )r%   r-   r   r   r!   num_processes   s    zBasePool.num_processes)NTTr   N)N)NN),rU   rT   __qualname____doc__rI   rK   r@   r
   TimerZsignal_safeZis_greenrA   _poolrH   Zuses_semaphoreZtask_join_will_blockZbody_can_be_bufferr,   r.   r/   r0   r1   r2   r4   r5   r7   r8   r9   r>   r?   rB   rC   rJ   rM   rL   rO   r   strr   rV   propertyrW   rX   rY   r   r   r   r!   r   /   sT       
	



r   )r[   rF   osr   timetypingr   r   Zbilliard.einfor   Zbilliard.exceptionsr   Zkombu.utils.encodingr   Zcelery.exceptionsr   r   r	   Zcelery.utilsr
   Zcelery.utils.logr   Zcelery.utils.textr   __all__rD   r   r   r   r   r   r   r   r!   <module>   s,      
