
    Yf                        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       H/var/www/html/env/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    ( VV
')) $'55
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*     #dj//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    1XX , ,++-->FF##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%5a%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DJ$/1A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!!!T5G5G5I5I> 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NNaNNJf%%%KKMMMMM  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 70 Wz z z z z z z z z zr   