§
    Ètºf¤*  ã                  ó(  — d Z ddlmZ ddlmZmZ ddlmZmZm	Z	m
Z
mZ ddlmZmZ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d¦  «        Z ed¦  «        Z G d„ de¦  «        Z G d„ de¦  «        Z e ¦   «         Z! G 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.
é    )Úannotations)ÚThreadÚcurrent_thread)ÚAnyÚCallableÚListÚOptionalÚTypeVar)Ú	ParamSpecÚProtocolÚ	TypedDict)Úpool)ÚcontextÚlog)Ú
deprecated)ÚFailure)ÚVersionÚ_PÚ_Rc                  ó   — e Zd Zdd„ZdS )Ú_SupportsQsizeÚreturnÚintc                ó   — d S ©N© ©Úselfs    úX/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/twisted/python/threadpool.pyÚqsizez_SupportsQsize.qsize   s   € Øˆó    N©r   r   ©Ú__name__Ú
__module__Ú__qualname__r    r   r!   r   r   r      s(   € € € € € ðð ð ð ð ð r!   r   c                  ó$   — e Zd ZU ded<   ded<   dS )Ú_Stater   ÚminÚmaxN)r$   r%   r&   Ú__annotations__r   r!   r   r(   r(   "   s"   € € € € € € Ø€H€H�HØ€H€H�H€H€Hr!   r(   c                  ób  — 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
¬¦  «        e¦  «        ¦  «        Z ee¦  «        Z	 d2d3d„Zed4d„¦   «         Zed5d„¦   «         Zed5d„¦   «         Zed6d„¦   «         ZeZd7d„Zd7d„Zd8d„Zd7d„Zd9d!„Zd:d"„Zd;d)„Zd<d-„Zd7d.„Z 	 d=d>d0„Z!d7d1„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ÚreplacementÚ
minthreadsr   Ú
maxthreadsÚnameúOptional[str]c                óÎ   ‡ — |dk    s
J d¦   «         ‚||k    s
J d¦   «         ‚|‰ _         |‰ _        |‰ _        g ‰ _        dˆ fd	„}dˆ f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 maximumÚar   Úkwr   r   c                 ó~   •—  ‰j         | d‰                     ¦   «         i|¤Ž}‰j                             |¦  «         |S )Nr7   )ÚthreadFactoryÚ_generateNameÚthreadsÚappend)r<   r=   Úthreadr   s      €r   ÚtrackingThreadFactoryz2ThreadPool.__init__.<locals>.trackingThreadFactory`   sS   ø€ Ø'�TÔ'ØðØ×+Ò+Ñ-Ô-ðØ13ðð ˆFð ŒL×Ò Ñ'Ô'Ð'ØˆMr!   r   c                 ó$   •— ‰ j         sdS ‰ j        S )Nr   )Ústartedr*   r   s   €r   ÚcurrentLimitz)ThreadPool.__init__.<locals>.currentLimitg   s   ø€ Ø”<ð Ø�qØ”8ˆOr!   N)r<   r   r=   r   r   r   r"   )r)   r*   r7   rA   Ú_poolÚ_team)r   r5   r6   r7   rD   rG   s   `     r   Ú__init__zThreadPool.__init__J   s§   ø€ ð ˜QŠˆˆÐ 5‰ŒˆØ˜ZÒ'Ð'Ð'Ð)JÑ'Ô'Ð'ØˆŒØˆŒØˆŒ	Ø%'ˆŒð	ð 	ð 	ð 	ð 	ð 	ð	ð 	ð 	ð 	ð 	ð 	ð
 —Z’Z Ð.CÑDÔDˆŒ
ˆ
ˆ
r!   r   c                óR   — | j                              ¦   «         }|j        |j        z   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}
        )rI   Ú
statisticsÚidleWorkerCountÚbusyWorkerCount)r   Ústatss     r   ÚworkerszThreadPool.workersn   s(   € ð ”
×%Ò%Ñ'Ô'ˆØÔ$ uÔ'<Ñ<Ð<r!   ú
list[None]c                óF   — dg| j                              ¦   «         j        z  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)rI   rL   rN   r   s    r   ÚworkingzThreadPool.workingz   s"   € ð ˆv˜œ
×-Ò-Ñ/Ô/Ô?Ñ?Ð?r!   c                óF   — dg| j                              ¦   «         j        z  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)rI   rL   rM   r   s    r   ÚwaiterszThreadPool.waiters…   s"   € ð ˆv˜œ
×-Ò-Ñ/Ô/Ô?Ñ?Ð?r!   r   c                ó6   ‡ —  G ˆ f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                  ó   •— e Zd Zdˆ fd„ZdS )ú$ThreadPool._queue.<locals>.NotAQueuer   r   c                ó@   •— ‰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}
                )rI   rL   ÚbackloggedWorkCount)Úqr   s    €r   r    z*ThreadPool._queue.<locals>.NotAQueue.qsize›   s   ø€ ð ”z×,Ò,Ñ.Ô.ÔBÐBr!   Nr"   r#   r   s   €r   Ú	NotAQueuerX   š   s;   ø€ € € € € ð	Cð 	Cð 	Cð 	Cð 	Cð 	Cð 	Cð 	Cr!   r\   r   )r   r\   s   ` r   Ú_queuezThreadPool._queue‘   sM   ø€ ð
	Cð 
	Cð 
	Cð 
	Cð 
	Cð 
	Cð 
	Cñ 
	Cô 
	Cð 
	Cð ˆy‰{Œ{Ðr!   ÚNonec                óÂ   — d| _         d| _        |                      ¦   «          | j                             ¦   «         j        }|r| j                             |¦  «         dS dS )z'
        Start the threadpool.
        FTN)ÚjoinedrF   ÚadjustPoolsizerI   rL   rZ   Úgrow)r   Úbacklogs     r   ÚstartzThreadPool.start«   sf   € ð ˆŒØˆŒà×ÒÑÔÐØ”*×'Ò'Ñ)Ô)Ô=ˆØð 	%ØŒJ�OŠO˜GÑ$Ô$Ð$Ð$Ð$ð	%ð 	%r!   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}.
        r2   N)rI   rb   r   s    r   ÚstartAWorkerzThreadPool.startAWorker·   s   € ð
 	Œ
�Š˜ÑÔÐÐÐr!   Ústrc                óD   — 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-ú-)r7   ÚidrP   r   s    r   r@   zThreadPool._generateName¾   s*   € ð D˜TœYÐ2­"¨T©(¬(ÐCÐC°T´\ÐCÐCÐCr!   c                ó:   — | j                              d¦  «         dS )zn
        Decrease the number of available workers by 1, by quitting one as soon
        as it's idle.
        r2   N)rI   Úshrinkr   s    r   ÚstopAWorkerzThreadPool.stopAWorkerÇ   s    € ð
 	Œ
×Ò˜!ÑÔÐÐÐr!   Ústater(   c                ót   — t          | d|¦  «         t                               | | j        | j        ¦  «         d S )NÚ__dict__)Úsetattrr-   rJ   r)   r*   )r   rn   s     r   Ú__setstate__zThreadPool.__setstate__Î   s6   € Ý��j %Ñ(Ô(Ð(Ý×Ò˜D $¤(¨D¬HÑ5Ô5Ð5Ð5Ð5r!   c                ó8   — t          | j        | j        ¬¦  «        S )N)r)   r*   )r(   r)   r*   r   s    r   Ú__getstate__zThreadPool.__getstate__Ò   s   € Ý˜$œ(¨¬Ð1Ñ1Ô1Ð1r!   ÚfuncúCallable[_P, object]Úargsú_P.argsr=   ú	_P.kwargsc                ó*   —  | 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   ru   rw   r=   s       r   ÚcallInThreadzThreadPool.callInThreadÕ   s/   € ð 	&ˆÔ% d¨DÐ>°4Ð>Ð>Ð>¸2Ð>Ð>Ð>Ð>Ð>r!   ÚonResultú&Optional[Callable[[bool, _R], object]]úCallable[_P, _R]c                óÜ   ‡‡‡‡‡— | j         rdS t          j                             ¦   «         j        d         Šdˆfd„Šˆˆˆˆf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éÿÿÿÿr   r^   c                 ó  •— 	 ‰                      ¦   «         } d}n # t          $ r t          ¦   «         } d}Y nw xY wd ‰_         ‰j        �‰                     || ¦  «         d ‰_        d S |st	          j        | ¦  «         d S d S )NTF)ÚtheWorkÚBaseExceptionr   r}   r   Úerr)ÚresultÚokÚ	inContexts     €r   rˆ   z6ThreadPool.callInThreadWithCallback.<locals>.inContext  s§   ø€ ðØ"×*Ò*Ñ,Ô,�Ø��øÝ ð ð ð Ý ™œ�Ø���ðøøøð !%ˆIÔØÔ!Ð-Ø×"Ò" 2 vÑ.Ô.Ð.Ø%)�	Ô"Ð"Ð"Øð  Ý”˜‘”���ð ð  s   ƒ š7¶7c                 ó0   •— t          j        ‰‰g‰ ¢R i ‰¤ŽS r   )r   Úcall)rw   Úctxru   r=   s   €€€€r   ú<lambda>z5ThreadPool.callInThreadWithCallback.<locals>.<lambda>  s2   ø€ ¥G¤LØ�ð%
Øð%
ð %
ð %
Ø "ð%
ð %
€ r!   ©r   r^   )	r`   r   ÚtheContextTrackerÚcurrentContextÚcontextsrƒ   r}   rI   Údo)r   r}   ru   rw   r=   r‹   rˆ   s     ```@@r   r{   z#ThreadPool.callInThreadWithCallbackã   sŸ   øøøøø€ ðH Œ;ð 	ØˆFÝÔ'×6Ò6Ñ8Ô8ÔAÀ"ÔEˆð	 ð 	 ð 	 ð 	 ð 	 ð 	 ð$
ð 
ð 
ð 
ð 
ð 
ð 
ˆ	Ôð &ˆ	ÔàŒ
�Š�iÑ Ô Ð Ð Ð r!   c                ó�   — d| _         d| _        | j                             ¦   «          | j        D ]}|                     ¦   «          ŒdS )z9
        Shutdown the threads in the threadpool.
        TFN)r`   rF   rI   ÚquitrA   Újoin)r   rC   s     r   ÚstopzThreadPool.stop$  sN   € ð ˆŒØˆŒØŒ
�ŠÑÔÐØ”lð 	ð 	ˆFØ�KŠK‰MŒMˆMˆMð	ð 	r!   úOptional[int]c                óx  — |€| j         }|€| j        }|dk    s
J d¦   «         ‚||k    s
J d¦   «         ‚|| _         || _        | j        sdS | j        | j        k    r'| j                             | j        | j        z
  ¦  «         | j        | j         k     r)| j                             | j         | j        z
  ¦  «         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*   rF   rP   rI   rl   rb   )r   r5   r6   s      r   ra   zThreadPool.adjustPoolsize.  sÓ   € ð ÐØœˆJØÐØœˆJà˜QŠˆˆÐ 5‰ŒˆØ˜ZÒ'Ð'Ð'Ð)JÑ'Ô'Ð'àˆŒØˆŒØŒ|ð 	ØˆFð Œ<˜$œ(Ò"Ð"ØŒJ×Ò˜dœl¨T¬XÑ5Ñ6Ô6Ð6àŒ<˜$œ(Ò"Ð"ØŒJ�OŠO˜DœH t¤|Ñ3Ñ4Ô4Ð4Ð4Ð4ð #Ð"r!   c                ó®   — t          j        d| j        › �¦  «         t          j        d| j        › �¦  «         t          j        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   ÚmsgrU   rS   rA   r   s    r   Ú	dumpStatszThreadPool.dumpStatsM  s[   € õ
 	ŒÐ*˜DœLÐ*Ð*Ñ+Ô+Ð+ÝŒÐ*˜DœLÐ*Ð*Ñ+Ô+Ð+ÝŒÐ(˜$œ,Ð(Ð(Ñ)Ô)Ð)Ð)Ð)r!   )r.   r/   N)r5   r   r6   r   r7   r8   r"   )r   rQ   )r   r   r�   )r   rg   )rn   r(   r   r^   )r   r(   )ru   rv   rw   rx   r=   ry   r   r^   )
r}   r~   ru   r   rw   rx   r=   ry   r   r^   )NN)r5   r–   r6   r–   r   r^   )#r$   r%   r&   Ú__doc__r)   r*   r`   rF   r7   r   r?   Ústaticmethodr   r   r   ÚcurrentThreadrH   rJ   ÚpropertyrP   rS   rU   r]   r[   rd   rf   r@   rm   rr   rt   r|   r{   r•   ra   rš   r   r!   r   r-   r-   *   s?  € € € € € ðð ð  €CØ
€CØ€FØ€GØ€Dà€MØ �Lð	
ˆ
ˆ
Ø�G˜I r¨1¨aÑ0Ô0Ø2ð	
ñ 	
ô 	
ð ñ	ô 	ñô €Mð ˆL˜ÑÔ€Eð PTð"Eð "Eð "Eð "Eð "EðH ð	=ð 	=ð 	=ñ „Xð	=ð ð@ð @ð @ñ „Xð@ð ð	@ð 	@ð 	@ñ „Xð	@ð ðð ð ñ „Xðð, 	€Að
%ð 
%ð 
%ð 
%ðð ð ð ðDð Dð Dð Dðð ð ð ð6ð 6ð 6ð 6ð2ð 2ð 2ð 2ð?ð ?ð ?ð ?ð?!ð ?!ð ?!ð ?!ðBð ð ð ð MQð5ð 5ð 5ð 5ð 5ð>*ð *ð *ð *ð *ð *r!   r-   N)#r›   Ú
__future__r   Ú	threadingr   r   Útypingr   r   r   r	   r
   Útyping_extensionsr   r   r   Útwisted._threadsr   rH   Útwisted.pythonr   r   Útwisted.python.deprecater   Útwisted.python.failurer   Útwisted.python.versionsr   r   r   r   r(   ÚobjectÚ
WorkerStopr-   r   r!   r   ú<module>rª      sš  ðð
ð ð #Ð "Ð "Ð "Ð "Ð "à ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9à <Ð <Ð <Ð <Ð <Ð <Ð <Ð <Ð <Ð <à *Ð *Ð *Ð *Ð *Ð *Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ø /Ð /Ð /Ð /Ð /Ð /Ø *Ð *Ð *Ð *Ð *Ð *Ø +Ð +Ð +Ð +Ð +Ð +à€Yˆt�_„_€Ø€WˆT�]„]€ðð ð ð ð �Xñ ô ð ð
ð ð ð ð ˆYñ ô ð ð
 ˆV‰XŒX€
ðj*ð j*ð j*ð j*ð j*ñ j*ô j*ð j*ð j*ð j*r!   