§
    Ètºf  ã                  ó¼   — d 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d	lmZ  G d
„ d¦  «        Z e
e¦  «         G d„ d¦  «        ¦   «         ZdS )zZ
Implementation of a L{Team} of workers; a thread-pool that can allocate work to
workers.
é    )Úannotations)Údeque)ÚCallableÚOptionalÚSet)Úimplementeré   )ÚIWorker)ÚQuit)ÚIExclusiveWorkerc                  ó   — e Zd ZdZd
d„Zd	S )Ú
StatisticsaÓ  
    Statistics about a L{Team}'s current activity.

    @ivar idleWorkerCount: The number of idle workers.
    @type idleWorkerCount: L{int}

    @ivar busyWorkerCount: The number of busy workers.
    @type busyWorkerCount: L{int}

    @ivar backloggedWorkCount: The number of work items passed to L{Team.do}
        which have not yet been sent to a worker to be performed because not
        enough workers are available.
    @type backloggedWorkCount: L{int}
    ÚidleWorkerCountÚintÚbusyWorkerCountÚbackloggedWorkCountÚreturnÚNonec                ó0   — || _         || _        || _        d S ©N)r   r   r   )Úselfr   r   r   s       úU/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/twisted/_threads/_team.pyÚ__init__zStatistics.__init__%   s"   € ð  /ˆÔØ.ˆÔØ#6ˆÔ Ð Ð ó    N)r   r   r   r   r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   © r   r   r   r      s2   € € € € € ðð ð7ð 7ð 7ð 7ð 7ð 7r   r   c                  ó^   — e Zd ZdZdd„Zdd„Zdd„Zdd d„Zdd d„Zd!d„Z	d"d„Z
d#d„Zd$d„ZdS )%ÚTeamax  
    A composite L{IWorker} implementation.

    @ivar _quit: A L{Quit} flag indicating whether this L{Team} has been quit
        yet.  This may be set by an arbitrary thread since L{Team.quit} may be
        called from anywhere.

    @ivar _coordinator: the L{IExclusiveWorker} coordinating access to this
        L{Team}'s internal resources.

    @ivar _createWorker: a callable that will create new workers.

    @ivar _logException: a 0-argument callable called in an exception context
        when there is an unhandled error from a task passed to L{Team.do}

    @ivar _idle: a L{set} of idle workers.

    @ivar _busyCount: the number of workers currently busy.

    @ivar _pending: a C{deque} of tasks - that is, 0-argument callables passed
        to L{Team.do} - that are outstanding.

    @ivar _shouldQuitCoordinator: A flag indicating that the coordinator should
        be quit at the next available opportunity.  Unlike L{Team._quit}, this
        flag is only set by the coordinator.

    @ivar _toShrink: the number of workers to shrink this L{Team} by at the
        next available opportunity; set in the coordinator.
    Úcoordinatorr   ÚcreateWorkerúCallable[[], Optional[IWorker]]ÚlogExceptionúCallable[[], None]c                óÌ   — t          ¦   «         | _        || _        || _        || _        t          ¦   «         | _        d| _        t          ¦   «         | _	        d| _
        d| _        dS )a   
        @param coordinator: an L{IExclusiveWorker} which will coordinate access
            to resources on this L{Team}; that is to say, an
            L{IExclusiveWorker} whose C{do} method ensures that its given work
            will be executed in a mutually exclusive context, not in parallel
            with other work enqueued by C{do} (although possibly in parallel
            with the caller).

        @param createWorker: A 0-argument callable that will create an
            L{IWorker} to perform work.

        @param logException: A 0-argument callable called in an exception
            context when the work passed to C{do} raises an exception.
        r   FN)r   Ú_quitÚ_coordinatorÚ_createWorkerÚ_logExceptionÚsetÚ_idleÚ
_busyCountr   Ú_pendingÚ_shouldQuitCoordinatorÚ	_toShrink)r   r"   r#   r%   s       r   r   zTeam.__init__M   sY   € õ( ‘V”VˆŒ
Ø'ˆÔØ)ˆÔØ)ˆÔõ $'¡5¤5ˆŒ
ØˆŒÝ8=¹¼ˆŒØ&+ˆÔ#ØˆŒˆˆr   r   r   c                óv   — t          t          | j        ¦  «        | j        t          | j        ¦  «        ¦  «        S )z›
        Gather information on the current status of this L{Team}.

        @return: a L{Statistics} describing the current state of this L{Team}.
        )r   Úlenr-   r.   r/   ©r   s    r   Ú
statisticszTeam.statisticsm   s*   € õ �#˜dœj™/œ/¨4¬?½CÀÄÑ<NÔ<NÑOÔOÐOr   Únr   r   c                ón   ‡ ‡— ‰ j                              ¦   «          ‰ j        j        dˆˆ fd„¦   «         }dS )z—
        Increase the the number of idle workers by C{n}.

        @param n: The number of new idle workers to create.
        @type n: L{int}
        r   r   c                 óˆ   •— t          ‰¦  «        D ]0} ‰                     ¦   «         }|€ d S ‰                     |¦  «         Œ1d S r   )Úranger*   Ú_recycleWorker)ÚxÚworkerr6   r   s     €€r   ÚcreateOneWorkerz"Team.grow.<locals>.createOneWorker~   sV   ø€ å˜1‘X”Xð ,ð ,�Ø×+Ò+Ñ-Ô-�Ø�>Ø�F�FØ×#Ò# FÑ+Ô+Ð+Ð+ð	,ð ,r   N©r   r   ©r(   Úcheckr)   Údo)r   r6   r=   s   `` r   Úgrowz	Team.growu   sX   øø€ ð 	Œ
×ÒÑÔÐà	Ô	Ô	ð	,ð 	,ð 	,ð 	,ð 	,ð 	,ñ 
Ô	ð	,ð 	,ð 	,r   NúOptional[int]c                óx   ‡ ‡— ‰ j                              ¦   «          ‰ j                             ˆˆ fd„¦  «         dS )zß
        Decrease the number of idle workers by C{n}.

        @param n: The number of idle workers to shut down, or L{None} (or
            unspecified) to shut down all workers.
        @type n: L{int} or L{None}
        c                 ó.   •— ‰                      ‰ ¦  «        S r   )Ú_quitIdlers)r6   r   s   €€r   ú<lambda>zTeam.shrink.<locals>.<lambda>�   s   ø€  T×%5Ò%5°aÑ%8Ô%8€ r   Nr?   )r   r6   s   ``r   ÚshrinkzTeam.shrink†   sD   øø€ ð 	Œ
×ÒÑÔÐØÔ×ÒÐ8Ð8Ð8Ð8Ð8Ñ9Ô9Ð9Ð9Ð9r   c                óJ  — |€t          | j        ¦  «        | j        z   }t          |¦  «        D ]E}| j        r,| j                             ¦   «                              ¦   «          Œ5| xj        dz  c_        ŒF| j        r&| j        dk    r| j                             ¦   «          dS dS dS )z|
        The implmentation of C{shrink}, performed by the coordinator worker.

        @param n: see L{Team.shrink}
        Nr	   r   )	r3   r-   r.   r9   ÚpopÚquitr1   r0   r)   )r   r6   r;   s      r   rF   zTeam._quitIdlers‘   s±   € ð ˆ9Ý�D”J‘” $¤/Ñ1ˆAÝ�q‘”ð 	$ð 	$ˆAØŒzð $Ø”
—’Ñ Ô ×%Ò%Ñ'Ô'Ð'Ð'à�” !Ñ#�”�ØÔ&ð 	%¨4¬?¸aÒ+?Ð+?ØÔ×"Ò"Ñ$Ô$Ð$Ð$Ð$ð	%ð 	%Ð+?Ð+?r   Útaskc                óx   ‡ ‡— ‰ j                              ¦   «          ‰ j                             ˆ ˆfd„¦  «         dS )zu
        Perform some work in a worker created by C{createWorker}.

        @param task: the callable to run
        c                 ó.   •— ‰                       ‰¦  «        S r   )Ú_coordinateThisTask©r   rL   s   €€r   rG   zTeam.do.<locals>.<lambda>¨   s   ø€  T×%=Ò%=¸dÑ%CÔ%C€ r   Nr?   rP   s   ``r   rA   zTeam.do¡   sD   øø€ ð 	Œ
×ÒÑÔÐØÔ×ÒÐCÐCÐCÐCÐCÑDÔDÐDÐDÐDr   úCallable[..., object]c                óþ   ‡ ‡‡— ‰ j         r‰ j                              ¦   «         n‰                      ¦   «         }|€‰ j                             ‰¦  «         dS |Š‰ xj        dz  c_        |j        dˆˆ ˆfd„¦   «         }dS )zø
        Select a worker to dispatch to, either an idle one or a new one, and
        perform it.

        This method should run on the coordinator worker.

        @param task: the task to dispatch
        @type task: 0-argument callable
        Nr	   r   r   c                 óš   •— 	  ‰¦   «          n$# t           $ r ‰                     ¦   «          Y nw xY w‰j        j        dˆˆfd„¦   «         } d S )Nr   r   c                 óR   •— ‰xj         dz  c_         ‰                     ‰ ¦  «         d S )Nr	   )r.   r:   )Únot_none_workerr   s   €€r   ÚidleAndPendingz@Team._coordinateThisTask.<locals>.doWork.<locals>.idleAndPendingÄ   s-   ø€ à�” 1Ñ$�”Ø×#Ò# OÑ4Ô4Ð4Ð4Ð4r   r>   )ÚBaseExceptionr+   r)   rA   )rV   rU   r   rL   s    €€€r   ÚdoWorkz(Team._coordinateThisTask.<locals>.doWork½   sˆ   ø€ ð%Ø�‘”��øÝ ð %ð %ð %Ø×"Ò"Ñ$Ô$Ð$Ð$Ð$ð%øøøð ÔÔ!ð5ð 5ð 5ð 5ð 5ð 5ñ "Ô!ð5ð 5ð 5s   ƒ
 Ž/®/r>   )r-   rJ   r*   r/   Úappendr.   rA   )r   rL   r<   rX   rU   s   ``  @r   rO   zTeam._coordinateThisTaskª   s¥   øøø€ ð &*¤ZÐI�”—’Ñ!Ô!Ð!°T×5GÒ5GÑ5IÔ5IˆØˆ>ð ŒM× Ò  Ñ&Ô&Ð&ØˆFØ ˆØˆŒ˜1ÑˆŒà	Œð		5ð 		5ð 		5ð 		5ð 		5ð 		5ð 		5ñ 
Œð		5ð 		5ð 		5r   r<   r
   c                ót  — | j                              |¦  «         | j        r.|                      | j                             ¦   «         ¦  «         dS | j        r|                      ¦   «          dS | j        dk    r@| xj        dz  c_        | j                              |¦  «         | 	                    ¦   «          dS dS )zÐ
        Called only from coordinator.

        Recycle the given worker into the idle pool.

        @param worker: a worker created by C{createWorker} and now idle.
        @type worker: L{IWorker}
        r   r	   N)
r-   Úaddr/   rO   Úpopleftr0   rF   r1   ÚremoverK   )r   r<   s     r   r:   zTeam._recycleWorkerÉ   s»   € ð 	Œ
�Š�vÑÔÐØŒ=ð 		ð ×$Ò$ T¤]×%:Ò%:Ñ%<Ô%<Ñ=Ô=Ð=Ð=Ð=ØÔ(ð 	Ø×ÒÑÔÐÐÐØŒ^˜aÒÐØˆNŒN˜aÑˆNŒNØŒJ×Ò˜fÑ%Ô%Ð%Ø�KŠK‰MŒMˆMˆMˆMð  Ðr   c                ój   ‡ — ‰ j                              ¦   «          ‰ j        j        dˆ fd„¦   «         }dS )zA
        Stop doing work and shut down all idle workers.
        r   r   c                 ó>   •— d‰ _         ‰                      ¦   «          d S )NT)r0   rF   r4   s   €r   ÚstartFinishingz!Team.quit.<locals>.startFinishingå   s#   ø€ à*.ˆDÔ'Ø×ÒÑÔÐÐÐr   Nr>   )r(   r,   r)   rA   )r   r`   s   ` r   rK   z	Team.quitÞ   sR   ø€ ð 	Œ
�ŠÑÔÐð 
Ô	Ô	ð	ð 	ð 	ð 	ð 	ñ 
Ô	ð	ð 	ð 	r   )r"   r   r#   r$   r%   r&   )r   r   )r6   r   r   r   r   )r6   rC   r   r   )rL   r&   r   r   )rL   rQ   r   r   )r<   r
   r   r   r>   )r   r   r   r   r   r5   rB   rH   rF   rA   rO   r:   rK   r   r   r   r!   r!   -   så   € € € € € ðð ð<ð ð ð ð@Pð Pð Pð Pð,ð ,ð ,ð ,ð"	:ð 	:ð 	:ð 	:ð 	:ð%ð %ð %ð %ð %ð Eð Eð Eð Eð5ð 5ð 5ð 5ð>ð ð ð ð*
ð 
ð 
ð 
ð 
ð 
r   r!   N)r   Ú
__future__r   Úcollectionsr   Útypingr   r   r   Úzope.interfacer   Ú r
   Ú_conveniencer   Ú	_ithreadsr   r   r!   r   r   r   ú<module>rh      s  ðð
ð ð #Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ð Ð Ø *Ð *Ð *Ð *Ð *Ð *Ð *Ð *Ð *Ð *à &Ð &Ð &Ð &Ð &Ð &à Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'ð7ð 7ð 7ð 7ð 7ñ 7ô 7ð 7ð0 €ˆWÑÔðzð zð zð zð zñ zô zñ Ôðzð zð zr   