HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //proc/self/root/usr/lib/python3/dist-packages/twisted/python/__pycache__/threadpool.cpython-38.pyc
U

��W[p&�@sldZddlmZmZddlZddlmZddlm	Z	m
Z
ddlmZddl
mZe�ZeGdd	�d	��ZdS)
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�eZdZdZdZdZdZdZdZdZ	e
jZe
e
j�Ze
e�Zd'dd�Zed	d
��Zedd��Zed
d��Zedd��ZeZdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd(d#d$�Zd%d&�Z dS))�
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}
    ��FrNcsb|dkstd��||ks td��|�_|�_|�_g�_�fdd�}�fdd�}��||��_dS)	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 maximumcs(�j|d���i|��}�j�|�|S)N�name)�
threadFactory�
_generateName�threads�append)�a�kw�thread��self��;/usr/lib/python3/dist-packages/twisted/python/threadpool.py�trackingThreadFactoryJsz2ThreadPool.__init__.<locals>.trackingThreadFactorycs�js
dS�jS)Nr)�started�maxrrrr�currentLimitOsz)ThreadPool.__init__.<locals>.currentLimitN)�AssertionError�minrrr�_pool�_team)r�
minthreads�
maxthreadsrrrrrr�__init__6s
zThreadPool.__init__cCs|j��}|j|jS)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)rZstatsrrr�workersWs	
zThreadPool.workerscCsdg|j��jS)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'rrrr�workingds	zThreadPool.workingcCsdg|j��jS)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&rrrr�waitersps
zThreadPool.waiterscsG�fdd�dt�}|�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.
        cseZdZ�fdd�ZdS)z$ThreadPool._queue.<locals>.NotAQueuecs�j��jS)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)�qrrr�qsize�s	z*ThreadPool._queue.<locals>.NotAQueue.qsizeN)�__name__�
__module__�__qualname__r-rrrr�	NotAQueue�sr1)�object)rr1rrr�_queue}szThreadPool._queuecCs4d|_d|_|��|j��j}|r0|j�|�dS)z'
        Start the threadpool.
        FTN)�joinedr�adjustPoolsizer!r%r+�grow)rZbacklogrrr�start�szThreadPool.startcCs|j�d�dS)z�
        Increase the number of available workers for the thread pool by 1, up
        to the maximum allowed by L{ThreadPool.max}.
        �N)r!r6rrrr�startAWorker�szThreadPool.startAWorkercCsd|jpt|�|jfS)z�
        Generate a name for a new pool thread.

        @return: A distinctive name for the thread.
        @rtype: native L{str}
        zPoolThread-%s-%s)r�idr(rrrrr�szThreadPool._generateNamecCs|j�d�dS)zn
        Decrease the number of available workers by 1, by quitting one as soon
        as it's idle.
        r8N)r!�shrinkrrrr�stopAWorker�szThreadPool.stopAWorkercCs"t|d|�t�||j|j�dS)N�__dict__)�setattrr	r$rr�r�staterrr�__setstate__�szThreadPool.__setstate__cCsi}|j|d<|j|d<|S)Nrr)rrr?rrr�__getstate__�s

zThreadPool.__getstate__cOs|jd|f|�|�dS)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�argsrrrr�callInThread�s
zThreadPool.callInThreadcsP|jr
dStj��jd��fdd������fdd��_|�_|j���dS)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���cs^z���}d}Wnt�}d}YnXd�_�jdk	rL��||�d�_n|sZt�|�dS)NTF)�theWorkr�onResultr�err)�result�ok)�	inContextrrrM�s

z6ThreadPool.callInThreadWithCallback.<locals>.inContextcstj��f����S)N)r�callr)rE�ctxrDrrr�<lambda>
�z5ThreadPool.callInThreadWithCallback.<locals>.<lambda>)	r4rZtheContextTrackerZcurrentContextZcontextsrHrIr!Zdo)rrIrDrErr)rErOrDrMrrrC�sz#ThreadPool.callInThreadWithCallbackcCs.d|_d|_|j��|jD]}|��qdS)z9
        Shutdown the threads in the threadpool.
        TFN)r4rr!�quitr�join)rrrrr�stops


zThreadPool.stopcCs�|dkr|j}|dkr|j}|dks,td��||ks<td��||_||_|jsRdS|j|jkrr|j�|j|j�|j|jkr�|j�|j|j�dS)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}.
        Nrrr
)rrrrr(r!r;r6)rr"r#rrrr5s	zThreadPool.adjustPoolsizecCs:t�d|jf�t�d|jf�t�d|jf�dS)zw
        Dump some plain-text informational messages to the log about the state
        of this L{ThreadPool}.
        zwaiters: %szworkers: %sz	total: %sN)r�msgr*r)rrrrr�	dumpStats9szThreadPool.dumpStats)r
rN)NN)!r.r/r0�__doc__rrr4rr(r�	threadingZThreadr�staticmethodZ
currentThreadr r$�propertyr)r*r3r,r7r9rr<rArBrFrCrTr5rVrrrrr	s>

!






:
r	)rWZ
__future__rrrXZtwisted._threadsrr Ztwisted.pythonrrZtwisted.python.failurerZtwisted.python._oldstylerr2Z
WorkerStopr	rrrr�<module>s