o
    ¯bÛ'  ã                   @   sx   d Z ddlmZmZ ddlmZ ddl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ƒ Z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.
é    )ÚThreadÚcurrent_thread)ÚList)Úpool)ÚcontextÚlog)Ú
deprecated)ÚFailure)ÚVersionc                   @   sæ   e Zd ZdZdZdZdZdZdZe	Z
eeedddd	ƒd
d�eƒƒ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}
    é   é   FNÚTwistedé   é   r   zthreading.current_thread)ÚversionÚreplacementc                    sb   |dksJ dƒ‚||ksJ 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ÚtrackingThreadFactoryL   s   ÿÿÿz2ThreadPool.__init__.<locals>.trackingThreadFactoryc                      s   ˆ j sdS ˆ jS )Nr   )ÚstartedÚmaxr   r   r   r    ÚcurrentLimitS   s   z)ThreadPool.__init__.<locals>.currentLimitN)Úminr#   r   r   Ú_poolÚ_team)r   Ú
minthreadsÚ
maxthreadsr   r!   r$   r   r   r    Ú__init__8   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   Ústatsr   r   r    ÚworkersZ   s   
	zThreadPool.workersc                 C   ó   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    Úworkingf   s   	zThreadPool.workingc                 C   r0   )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    Úwaitersq   s   
zThreadPool.waitersc                    s   G ‡ fdd„dƒ}|ƒ 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__r5   r   r   r   r    Ú	NotAQueue†   s    r9   r   )r   r9   r   r   r    Ú_queue}   s   	zThreadPool._queuec                 C   s8   d| _ d| _|  ¡  | j ¡ j}|r| j |¡ dS dS )z'
        Start the threadpool.
        FTN)Újoinedr"   ÚadjustPoolsizer'   r+   r3   Úgrow)r   Úbacklogr   r   r    Ústart—   s   ÿzThreadPool.startc                 C   ó   | 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}.
        r   N)r'   r=   r   r   r   r    ÚstartAWorker£   ó   zThreadPool.startAWorkerc                 C   s   d| j pt| ƒ› d| j› �S )z‹
        Generate a name for a new pool thread.

        @return: A distinctive name for the thread.
        @rtype: native L{str}
        zPoolThread-ú-)r   Úidr/   r   r   r   r    r   ª   s   zThreadPool._generateNamec                 C   r@   )zn
        Decrease the number of available workers by 1, by quitting one as soon
        as it's idle.
        r   N)r'   Úshrinkr   r   r   r    ÚstopAWorker³   rB   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#   rI   r   r   r    Ú__getstate__¾   s   

zThreadPool.__getstate__c                 O   s   | j d|g|¢R i |¤Ž 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                     sj   zˆ   ¡ } d}W n ty   tƒ } d}Y nw d ˆ _ ˆ jd ur*ˆ  || ¡ d ˆ _d S |s3t | ¡ d S d S )NTF)ÚtheWorkÚBaseExceptionr	   ÚonResultr   Úerr)ÚresultÚok)Ú	inContextr   r    rX   ò   s   þ

ÿz6ThreadPool.callInThreadWithCallback.<locals>.inContextc                      s   t jˆˆgˆ ¢R i ˆ¤ŽS )N)r   Úcallr   )rO   ÚctxrN   r   r   r    Ú<lambda>  s    ÿÿÿz5ThreadPool.callInThreadWithCallback.<locals>.<lambda>)	r;   r   ÚtheContextTrackerÚcurrentContextÚcontextsrR   rT   r'   Údo)r   rT   rN   rO   r   r   )rO   rZ   rN   rX   r   r    rM   Ð   s   z#ThreadPool.callInThreadWithCallbackc                 C   s.   d| _ d| _| j ¡  | jD ]}| ¡  qdS )z9
        Shutdown the threads in the threadpool.
        TFN)r;   r"   r'   Úquitr   Újoin)r   r   r   r   r    Ústop  s   


ÿzThreadPool.stopc                 C   sš   |du r| j }|du r| j}|dksJ dƒ‚||ksJ dƒ‚|| _ || _| js)dS | j| jkr9| j | j| j ¡ | j| j k rK| j | j | j ¡ dS 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'   rE   r=   )r   r(   r)   r   r   r    r<     s   	ÿzThreadPool.adjustPoolsizec                 C   s:   t  d| j› �¡ t  d| j› �¡ t  d| j› �¡ dS )zw
        Dump some plain-text informational messages to the log about the state
        of this L{ThreadPool}.
        z	waiters: z	workers: ztotal: N)r   Úmsgr2   r1   r   r   r   r   r    Ú	dumpStats2  s   zThreadPool.dumpStats)r   r   N)NN)#r6   r7   r8   Ú__doc__r%   r#   r;   r"   r   r   r   Ústaticmethodr   r
   r   ÚcurrentThreadr&   r*   Úpropertyr/   r1   r2   r:   r4   r?   rA   r   rF   rK   rL   rP   rM   rb   r<   rd   r   r   r   r    r      sL    þýÿ
"




	;

r   N)re   Ú	threadingr   r   Útypingr   Útwisted._threadsr   r&   Útwisted.pythonr   r   Útwisted.python.deprecater   Útwisted.python.failurer	   Útwisted.python.versionsr
   ÚobjectÚ
WorkerStopr   r   r   r   r    Ú<module>   s   