U
    ¹êW[p&  ã                   @   sl   d Z ddlmZm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 eƒ ZeG dd	„ d	ƒƒZdS )
zº
twisted.python.threadpool: a pool of threads to which we dispatch tasks.

In most cases you can just use C{reactor.callInThread} and friends
instead of creating a thread pool directly.
é    )ÚdivisionÚabsolute_importN)Úpool)ÚlogÚcontext)ÚFailure)Ú	_oldStylec                   @   sØ   e Zd ZdZdZdZdZdZdZdZ	e
jZee
jƒZeeƒZd'dd„Zed	d
„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZe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#d$„Zd%d&„ Z dS ))Ú
ThreadPoolaè  
    This class (hopefully) generalizes the functionality of a pool of threads
    to which work can be dispatched.

    L{callInThread} and L{stop} should only be called from a single thread.

    @ivar started: Whether or not the thread pool is currently running.
    @type started: L{bool}

    @ivar threads: List of workers currently running in this thread pool.
    @type threads: L{list}

    @ivar _pool: A hook for testing.
    @type _pool: callable compatible with L{_pool}
    é   é   Fr   Nc                    sb   |dkst dƒ‚||ks t dƒ‚|ˆ _|ˆ _|ˆ _g ˆ _‡ fdd„}‡ fdd„}ˆ  ||¡ˆ _dS )	ac  
        Create a new threadpool.

        @param minthreads: minimum number of threads in the pool
        @type minthreads: L{int}

        @param maxthreads: maximum number of threads in the pool
        @type maxthreads: L{int}

        @param name: The name to give this threadpool; visible in log messages.
        @type name: native L{str}
        r   úminimum is negativeúminimum is greater than maximumc                     s(   ˆ j | dˆ  ¡ i|—Ž}ˆ j |¡ |S )NÚname)ÚthreadFactoryÚ_generateNameÚthreadsÚappend)ÚaÚkwÚthread©Úself© ú;/usr/lib/python3/dist-packages/twisted/python/threadpool.pyÚtrackingThreadFactoryJ   s    z2ThreadPool.__init__.<locals>.trackingThreadFactoryc                      s   ˆ j s
dS ˆ jS )Nr   )ÚstartedÚmaxr   r   r   r   ÚcurrentLimitO   s    z)ThreadPool.__init__.<locals>.currentLimitN)ÚAssertionErrorÚminr   r   r   Ú_poolÚ_team)r   Ú
minthreadsÚ
maxthreadsr   r   r   r   r   r   Ú__init__6   s    zThreadPool.__init__c                 C   s   | j  ¡ }|j|j S )a  
        For legacy compatibility purposes, return a total number of workers.

        @return: the current number of workers, both idle and busy (but not
            those that have been quit by L{ThreadPool.adjustPoolsize})
        @rtype: L{int}
        )r!   Ú
statisticsÚidleWorkerCountÚbusyWorkerCount)r   Zstatsr   r   r   ÚworkersW   s    	
zThreadPool.workersc                 C   s   dg| j  ¡ j S )zý
        For legacy compatibility purposes, return the number of busy workers as
        expressed by a list the length of that number.

        @return: the number of workers currently processing a work item.
        @rtype: L{list} of L{None}
        N)r!   r%   r'   r   r   r   r   Úworkingd   s    	zThreadPool.workingc                 C   s   dg| j  ¡ j S )a,  
        For legacy compatibility purposes, return the number of idle workers as
        expressed by a list the length of that number.

        @return: the number of workers currently alive (with an allocated
            thread) but waiting for new work.
        @rtype: L{list} of L{None}
        N)r!   r%   r&   r   r   r   r   Úwaitersp   s    
zThreadPool.waitersc                    s   G ‡ fdd„dt ƒ}|ƒ S )zÙ
        For legacy compatibility purposes, return an object with a C{qsize}
        method that indicates the amount of work not yet allocated to a worker.

        @return: an object with a C{qsize} method.
        c                       s   e Zd Z‡ fdd„ZdS )z$ThreadPool._queue.<locals>.NotAQueuec                    s   ˆ j  ¡ jS )a  
                Pretend to be a Python threading Queue and return the
                number of as-yet-unconsumed tasks.

                @return: the amount of backlogged work not yet dispatched to a
                    worker.
                @rtype: L{int}
                )r!   r%   ÚbackloggedWorkCount)Úqr   r   r   Úqsize†   s    	z*ThreadPool._queue.<locals>.NotAQueue.qsizeN)Ú__name__Ú
__module__Ú__qualname__r-   r   r   r   r   Ú	NotAQueue…   s   r1   )Úobject)r   r1   r   r   r   Ú_queue}   s    zThreadPool._queuec                 C   s4   d| _ d| _|  ¡  | j ¡ j}|r0| j |¡ dS )z'
        Start the threadpool.
        FTN)Újoinedr   ÚadjustPoolsizer!   r%   r+   Úgrow)r   Zbacklogr   r   r   Ústart–   s    zThreadPool.startc                 C   s   | j  d¡ dS )zŒ
        Increase the number of available workers for the thread pool by 1, up
        to the maximum allowed by L{ThreadPool.max}.
        é   N)r!   r6   r   r   r   r   ÚstartAWorker£   s    zThreadPool.startAWorkerc                 C   s   d| j pt| ƒ| jf S )z‹
        Generate a name for a new pool thread.

        @return: A distinctive name for the thread.
        @rtype: native L{str}
        zPoolThread-%s-%s)r   Úidr(   r   r   r   r   r   «   s    zThreadPool._generateNamec                 C   s   | j  d¡ dS )zn
        Decrease the number of available workers by 1, by quitting one as soon
        as it's idle.
        r8   N)r!   Úshrinkr   r   r   r   ÚstopAWorkerµ   s    zThreadPool.stopAWorkerc                 C   s"   t | d|ƒ t | | j| j¡ d S )NÚ__dict__)Úsetattrr	   r$   r   r   ©r   Ústater   r   r   Ú__setstate__½   s    zThreadPool.__setstate__c                 C   s   i }| j |d< | j|d< |S )Nr   r   )r   r   r?   r   r   r   Ú__getstate__Â   s    

zThreadPool.__getstate__c                 O   s   | j d|f|ž|Ž dS )a   
        Call a callable object in a separate thread.

        @param func: callable object to be called in separate thread

        @param args: positional arguments to be passed to C{func}

        @param kw: keyword args to be passed to C{func}
        N)ÚcallInThreadWithCallback)r   ÚfuncÚargsr   r   r   r   ÚcallInThreadÉ   s    
zThreadPool.callInThreadc                    sP   | j r
dS tj ¡ jd ‰‡fdd„‰‡ ‡‡‡fdd„ˆ_|ˆ_| j ˆ¡ dS )a$  
        Call a callable object in a separate thread and call C{onResult} with
        the return value, or a L{twisted.python.failure.Failure} if the
        callable raises an exception.

        The callable is allowed to block, but the C{onResult} function must not
        block and should perform as little work as possible.

        A typical action for C{onResult} for a threadpool used with a Twisted
        reactor would be to schedule a L{twisted.internet.defer.Deferred} to
        fire in the main reactor thread using C{.callFromThread}.  Note that
        C{onResult} is called inside the separate thread, not inside the
        reactor thread.

        @param onResult: a callable with the signature C{(success, result)}.
            If the callable returns normally, C{onResult} is called with
            C{(True, result)} where C{result} is the return value of the
            callable.  If the callable throws an exception, C{onResult} is
            called with C{(False, failure)}.

            Optionally, C{onResult} may be L{None}, in which case it is not
            called at all.

        @param func: callable object to be called in separate thread

        @param args: positional arguments to be passed to C{func}

        @param kw: keyword arguments to be passed to C{func}
        Néÿÿÿÿc                     s^   zˆ   ¡ } d}W n   tƒ } d}Y nX d ˆ _ ˆ jd k	rLˆ  || ¡ d ˆ _n|sZt | ¡ d S )NTF)ÚtheWorkr   ÚonResultr   Úerr)ÚresultÚok)Ú	inContextr   r   rM   ø   s    

z6ThreadPool.callInThreadWithCallback.<locals>.inContextc                      s   t jˆˆfˆ žˆŽS )N)r   Úcallr   )rE   ÚctxrD   r   r   r   Ú<lambda>
  ó    z5ThreadPool.callInThreadWithCallback.<locals>.<lambda>)	r4   r   ZtheContextTrackerZcurrentContextZcontextsrH   rI   r!   Zdo)r   rI   rD   rE   r   r   )rE   rO   rD   rM   r   r   rC   Ö   s    z#ThreadPool.callInThreadWithCallbackc                 C   s.   d| _ d| _| j ¡  | jD ]}| ¡  qdS )z9
        Shutdown the threads in the threadpool.
        TFN)r4   r   r!   Úquitr   Újoin)r   r   r   r   r   Ústop  s
    

zThreadPool.stopc                 C   s–   |dkr| j }|dkr| j}|dks,tdƒ‚||ks<tdƒ‚|| _ || _| jsRdS | j| jkrr| j | j| j ¡ | j| j k r’| j | j | j ¡ dS )zî
        Adjust the number of available threads by setting C{min} and C{max} to
        new values.

        @param minthreads: The new value for L{ThreadPool.min}.

        @param maxthreads: The new value for L{ThreadPool.max}.
        Nr   r   r   )r   r   r   r   r(   r!   r;   r6   )r   r"   r#   r   r   r   r5     s    	zThreadPool.adjustPoolsizec                 C   s:   t  d| jf ¡ t  d| jf ¡ t  d| jf ¡ dS )zw
        Dump some plain-text informational messages to the log about the state
        of this L{ThreadPool}.
        zwaiters: %szworkers: %sz	total: %sN)r   Úmsgr*   r)   r   r   r   r   r   Ú	dumpStats9  s    zThreadPool.dumpStats)r
   r   N)NN)!r.   r/   r0   Ú__doc__r   r   r4   r   r(   r   Ú	threadingZThreadr   ÚstaticmethodZcurrentThreadr    r$   Úpropertyr)   r*   r3   r,   r7   r9   r   r<   rA   rB   rF   rC   rT   r5   rV   r   r   r   r   r	      s>   

!




:
r	   )rW   Z
__future__r   r   rX   Ztwisted._threadsr   r    Ztwisted.pythonr   r   Ztwisted.python.failurer   Ztwisted.python._oldstyler   r2   Z
WorkerStopr	   r   r   r   r   Ú<module>   s   