+
    Dfj                        R t ^ RIHt ^ RIHt ^ RIHtHtHt ^ RI	H
t
 ^RIHt ^RIHt ^RIHt  ! R R	4      t]
! ]4       ! R
 R4      4       tR# )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                  &    ] tR t^tRtR R ltRtR# )
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}
c               (    V ^8  d   QhRRRRRRRR/# )   idleWorkerCountintbusyWorkerCountbackloggedWorkCountreturnNone )formats   "8/usr/lib/python3/dist-packages/twisted/_threads/_team.py__annotate__Statistics.__annotate__%   s,     7 7"7587OR7	7    c                	*    Wn         W n        W0n        R # N)r   r   r   )selfr   r   r   s   &&&&r   __init__Statistics.__init__%   s      /.#6 r   )r   r   r   N)__name__
__module____qualname____firstlineno____doc__r   __static_attributes__r   r   r   r   r      s    7 7r   r   c                      ] tR t^-tRtR R ltR R ltR R ltRR	 R
 lltRR R llt	R R lt
R R ltR R ltR R ltRtR# )Teama,  
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.
c               $    V ^8  d   QhRRRRRR/# )r   coordinatorr
   createWorkerzCallable[[], Optional[IWorker]]logExceptionzCallable[[], None]r   )r   s   "r   r   Team.__annotate__M   s(      % 6 )	r   c                    \        4       V n        Wn        W n        W0n        \        4       V n        ^ V n        \        4       V n	        RV n
        ^ V n        R# )aH  
@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.
FN)r	   _quit_coordinator_createWorker_logExceptionset_idle
_busyCountr   _pending_shouldQuitCoordinator	_toShrink)r   r)   r*   r+   s   &&&&r   r   Team.__init__M   sI    ( V
')) $'5
8=&+#r   c                   V ^8  d   QhRR/# )r   r   r   r   )r   s   "r   r   r,   m   s     P PJ Pr   c                |    \        \        V P                  4      V P                  \        V P                  4      4      # )z
Gather information on the current status of this L{Team}.

@return: a L{Statistics} describing the current state of this L{Team}.
)r   lenr3   r4   r5   r   s   &r   
statisticsTeam.statisticsm   s(     #djj/4??C<NOOr   c                    V ^8  d   QhRRRR/# )r   nr   r   r   r   )r   s   "r   r   r,   u   s     , ,c ,d ,r   c                   a a S P                   P                  4        S P                  P                  R VV 3R ll4       pR# )zw
Increase the the number of idle workers by C{n}.

@param n: The number of new idle workers to create.
@type n: L{int}
c                   V ^8  d   QhRR/# r   r   r   r   )r   s   "r   r   Team.grow.<locals>.__annotate__   s     	, 	, 	,r   c                 |   < \        S4       F+  p SP                  4       pVf    R # SP                  V4       K-  	  R # r   )ranger0   _recycleWorker)xworkerr@   r   s     r   createOneWorker"Team.grow.<locals>.createOneWorker~   s5    1X++->##F+	 r   Nr.   checkr/   do)r   r@   rJ   s   ff r   grow	Team.growu   s8     	

						, 	, 
	,r   Nc                    V ^8  d   QhRRRR/# r   r@   zOptional[int]r   r   r   )r   s   "r   r   r,      s     	: 	: 	: 	:r   c                ~   a a S P                   P                  4        S P                  P                  VV 3R l4       R# )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P                  S 4      # r   )_quitIdlers)r@   r   s   r   <lambda>Team.shrink.<locals>.<lambda>   s    T%5%5a%8r   NrL   )r   r@   s   ffr   shrinkTeam.shrink   s*     	

89r   c                    V ^8  d   QhRRRR/# rR   r   )r   s   "r   r   r,      s     % %] %d %r   c                   Vf'   \        V P                  4      V P                  ,           p\        V4       FX  pV P                  '       d+   V P                  P	                  4       P                  4        K?  V ;P                  ^,          un        KZ  	  V P                  '       d0   V P                  ^ 8X  d   V P                  P                  4        R# R# R# )zd
The implmentation of C{shrink}, performed by the coordinator worker.

@param n: see L{Team.shrink}
N)	r;   r3   r4   rF   popquitr7   r6   r/   )r   r@   rH   s   && r   rU   Team._quitIdlers   s     9DJJ$//1AqAzzz

 %%'!#	 
 &&&4??a+?""$ ,@&r   c                    V ^8  d   QhRRRR/# )r   taskzCallable[[], object]r   r   r   )r   s   "r   r   r,      s     E E+ E Er   c                ~   a a S P                   P                  4        S P                  P                  V V3R l4       R# )z]
Perform some work in a worker created by C{createWorker}.

@param task: the callable to run
c                 &   < S P                  S4      # r   )_coordinateThisTaskr   r`   s   r   rV   Team.do.<locals>.<lambda>   s    T%=%=d%Cr   NrL   rd   s   ffr   rN   Team.do   s*     	

CDr   c                    V ^8  d   QhRRRR/# )r   r`   zCallable[..., object]r   r   r   )r   s   "r   r   r,      s     5 5(= 5$ 5r   c                0  a aa S P                   '       d   S P                   P                  4       MS P                  4       pVf   S P                  P	                  S4       R# VoS ;P
                  ^,          un        VP                  R VV V3R ll4       pR# )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
Nc                   V ^8  d   QhRR/# rC   r   )r   s   "r   r   .Team._coordinateThisTask.<locals>.__annotate__   s     		5 		5 		5r   c                    <  S! 4        SP                  P                  R  VV3R ll4       p R#   \          d    SP                  4         LBi ; i)c                   V ^8  d   QhRR/# rC   r   )r   s   "r   r   >Team._coordinateThisTask.<locals>.doWork.<locals>.__annotate__   s     5 5D 5r   c                 \   < S;P                   ^,          un         SP                  S 4       R# )   N)r4   rG   )not_none_workerr   s   r   idleAndPending@Team._coordinateThisTask.<locals>.doWork.<locals>.idleAndPending   s    1$##O4r   N)BaseExceptionr1   r/   rN   )rq   rp   r   r`   s    r   doWork(Team._coordinateThisTask.<locals>.doWork   sO    % !!5 5 "5	 ! %""$%s   / AA)r3   r\   r0   r5   appendr4   rN   )r   r`   rI   rt   rp   s   ff  @r   rc   Team._coordinateThisTask   sr     &*ZZZ!T5G5G5I> MM  & 1			5 		5 
		5r   c                    V ^8  d   QhRRRR/# )r   rI   r   r   r   r   )r   s   "r   r   r,      s      W  r   c                   V P                   P                  V4       V P                  '       d,   V P                  V P                  P	                  4       4       R# V P
                  '       d   V P                  4        R# V P                  ^ 8  dG   V ;P                  ^,          un        V P                   P                  V4       VP                  4        R# R# )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}
N)
r3   addr5   rc   popleftr6   rU   r7   remover]   )r   rI   s   &&r   rG   Team._recycleWorker   s     	

v=== $$T]]%:%:%<=(((^^aNNaNJJf%KKM  r   c                   V ^8  d   QhRR/# rC   r   )r   s   "r   r   r,      s     
 
d 
r   c                   a  S P                   P                  4        S P                  P                  R V 3R ll4       pR# )z1
Stop doing work and shut down all idle workers.
c                   V ^8  d   QhRR/# rC   r   )r   s   "r   r   Team.quit.<locals>.__annotate__   s     	 	 	r   c                 6   < R S n         S P                  4        R# )TN)r6   rU   r<   s   r   startFinishing!Team.quit.<locals>.startFinishing   s    *.D'r   N)r.   r2   r/   rN   )r   r   s   f r   r]   	Team.quit   s3     	

 
					 
	r   )	r4   r/   r0   r3   r1   r5   r.   r6   r7   r   )r    r!   r"   r#   r$   r   r=   rO   rX   rU   rN   rc   rG   r]   r%   r   r   r   r'   r'   -   s<    <@P,"	:% E5>*
 
r   r'   N)r$   
__future__r   collectionsr   typingr   r   r   zope.interfacer    r   _conveniencer	   	_ithreadsr
   r   r'   r   r   r   <module>r      sO   
 #  * * &   '7 70 Wz z zr   