ó
    Ö›‘h  ã                  ó¢   • S r SSKJr  SSKJr  SSKJrJrJr  SSK	J
r
  SSKJr  SSKJr  SS	KJr   " S
 S5      r\
" \5       " S S5      5       rg)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                  ó2   • \ rS rSrSr        SS jrSrg)Ú
Statisticsé   a«  
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                ó(   • Xl         X l        X0l        g ©N)ÚidleWorkerCountÚbusyWorkerCountÚbackloggedWorkCount)Úselfr   r   r   s       ÚZC:\Users\psant\Downloads\Noiseback\school\venv\Lib\site-packages\twisted/_threads/_team.pyÚ__init__ÚStatistics.__init__%   s   € ð  /ÔØ.ÔØ#6Õ ó    )r   r   r   N)r   Úintr   r   r   r   ÚreturnÚNone)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   Ú__static_attributes__© r   r   r   r      s,   † ñð7Ø"ð7Ø58ð7ØORð7à	÷7r   r   c                  ó†   • \ rS rSrSr      SS jrSS jrSS jrSSS jjrSSS jjr	SS	 jr
SS
 jrSS jrSS jrSrg)ÚTeamé-   a,  
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                ó¬   • [        5       U l        Xl        X l        X0l        [        5       U l        SU l        [        5       U l	        SU l
        SU l        g)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.
r   FN)r   Ú_quitÚ_coordinatorÚ_createWorkerÚ_logExceptionÚsetÚ_idleÚ
_busyCountr   Ú_pendingÚ_shouldQuitCoordinatorÚ	_toShrink)r   ÚcoordinatorÚcreateWorkerÚlogExceptions       r   r   ÚTeam.__init__M   sI   € ô( “VˆŒ
Ø'ÔØ)ÔØ)Ôô $'£5ˆŒ
ØˆŒÜ8=»ˆŒØ&+ˆÔ#Øˆ�r   c                ó|   • [        [        U R                  5      U R                  [        U R                  5      5      $ )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   Ú
statisticsÚTeam.statisticsm   s(   € ô œ#˜dŸj™j›/¨4¯?©?¼CÀÇÁÓ<NÓOÐOr   c                ó€   ^ ^• T R                   R                  5         T R                  R                  SUU 4S jj5       ng)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   >• [        T5       H)  n TR                  5       nUc    g TR                  U5        M+     g r   )Úranger*   Ú_recycleWorker)ÚxÚworkerÚnr   s     €€r   ÚcreateOneWorkerÚ"Team.grow.<locals>.createOneWorker~   s5   ø€ ä˜1–X�Ø×+Ñ+Ó-�Ø‘>ÙØ×#Ñ# FÖ+ò	 r   N©r   r   ©r(   Úcheckr)   Údo)r   rA   rB   s   `` r   ÚgrowÚ	Team.growu   s3   ù€ ð 	�
‰
×ÑÔà	×	Ñ	×	Ñ	÷	,ó 
ñ	,r   Nc                ó|   ^ ^• T R                   R                  5         T R                  R                  UU 4S j5        g)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                 ó&   >• TR                  T 5      $ r   )Ú_quitIdlers)rA   r   s   €€r   Ú<lambda>ÚTeam.shrink.<locals>.<lambda>�   s   ø€  T×%5Ñ%5°aÔ%8r   NrE   )r   rA   s   ``r   ÚshrinkÚTeam.shrink†   s*   ù€ ð 	�
‰
×ÑÔØ×Ñ×ÑÕ8Õ9r   c                óŒ  • Uc"  [        U R                  5      U R                  -   n[        U5       HS  nU R                  (       a*  U R                  R	                  5       R                  5         M>  U =R                  S-  sl        MU     U R                  (       a,  U R                  S:X  a  U R                  R                  5         ggg)zd
The implmentation of C{shrink}, performed by the coordinator worker.

@param n: see L{Team.shrink}
Nr	   r   )	r7   r-   r.   r=   ÚpopÚquitr1   r0   r)   )r   rA   r?   s      r   rL   ÚTeam._quitIdlers‘   sŠ   € ð ‰9Ü�D—J‘J“ $§/¡/Ñ1ˆAÜ�q–ˆAØ�z�zØ—
‘
—‘Ó ×%Ñ%Ö'à—’ !Ñ#—ñ	 ð
 ×&×&¨4¯?©?¸aÓ+?Ø×Ñ×"Ñ"Õ$ð ,@Ð&r   c                ó|   ^ ^• T R                   R                  5         T R                  R                  U U4S j5        g)z]
Perform some work in a worker created by C{createWorker}.

@param task: the callable to run
c                 ó&   >• T R                  T5      $ r   )Ú_coordinateThisTask©r   Útasks   €€r   rM   ÚTeam.do.<locals>.<lambda>¨   s   ø€  T×%=Ñ%=¸dÔ%Cr   NrE   rX   s   ``r   rG   ÚTeam.do¡   s*   ù€ ð 	�
‰
×ÑÔØ×Ñ×ÑÕCÕDr   c                ó  ^ ^^• T R                   (       a  T R                   R                  5       OT R                  5       nUc  T R                  R	                  T5        gUmT =R
                  S-  sl        UR                  SUU U4S jj5       ng)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	   c                 óš   >•  T" 5         TR                  R                  SUU4S jj5       n g ! [          a    TR                  5          N?f = f)Nc                 óR   >• T=R                   S-  sl         TR                  T 5        g )Nr	   )r.   r>   )Únot_none_workerr   s   €€r   ÚidleAndPendingÚ@Team._coordinateThisTask.<locals>.doWork.<locals>.idleAndPendingÄ   s   ø€ à—’ 1Ñ$•Ø×#Ñ# OÕ4r   rD   )ÚBaseExceptionr+   r)   rG   )r`   r_   r   rY   s    €€€r   ÚdoWorkÚ(Team._coordinateThisTask.<locals>.doWork½   sJ   ø€ ð%Ù”ð ×Ñ×!Ñ!÷5ó "ñ5øô	 !ó %Ø×"Ñ"Ö$ð%ús   ƒ- ­A
Á	A
rD   )r-   rR   r*   r/   Úappendr.   rG   )r   rY   r@   rc   r_   s   ``  @r   rW   ÚTeam._coordinateThisTaskª   sp   ú€ ð &*§Z§Z�—‘—‘Ô!°T×5GÑ5GÓ5IˆØ‰>ð �M‰M× Ñ  Ô&ØØ ˆØ�Š˜1Ñ�à	�‰÷		5ð 		5ó 
ñ		5r   c                ó–  • U R                   R                  U5        U R                  (       a*  U R                  U R                  R	                  5       5        gU R
                  (       a  U R                  5         gU R                  S:”  aA  U =R                  S-  sl        U R                   R                  U5        UR                  5         gg)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/   rW   Úpopleftr0   rL   r1   ÚremoverS   )r   r@   s     r   r>   ÚTeam._recycleWorkerÉ   s‡   € ð 	�
‰
�‰�vÔØ�=�=ð ×$Ñ$ T§]¡]×%:Ñ%:Ó%<Õ=Ø×(×(Ø×ÑÕØ�^‰^˜aÓØ�NŠN˜aÑ�NØ�J‰J×Ñ˜fÔ%Ø�K‰K�Mð  r   c                ó|   ^ • T R                   R                  5         T R                  R                  SU 4S jj5       ng)z1
Stop doing work and shut down all idle workers.
c                 ó4   >• ST l         T R                  5         g )NT)r0   rL   r8   s   €r   ÚstartFinishingÚ!Team.quit.<locals>.startFinishingå   s   ø€ à*.ˆDÔ'Ø×ÑÕr   NrD   )r(   r,   r)   rG   )r   rn   s   ` r   rS   Ú	Team.quitÞ   s3   ø€ ð 	�
‰
�‰Ôð 
×	Ñ	×	Ñ	ö	ó 
ñ	r   )	r.   r)   r*   r-   r+   r/   r(   r0   r1   )r2   r   r3   zCallable[[], Optional[IWorker]]r4   zCallable[[], None])r   r   )rA   r   r   r   r   )rA   zOptional[int]r   r   )rY   zCallable[[], object]r   r   )rY   zCallable[..., object]r   r   )r@   r
   r   r   rD   )r   r   r   r    r!   r   r9   rH   rO   rL   rG   rW   r>   rS   r"   r#   r   r   r%   r%   -   sS   † ñð<à%ðð 6ðð )ô	ô@Pô,ö"	:ö%ô Eô5ô>÷*
r   r%   N)r!   Ú
__future__r   Úcollectionsr   Útypingr   r   r   Úzope.interfacer   Ú r
   Ú_conveniencer   Ú	_ithreadsr   r   r%   r#   r   r   Ú<module>rx      sO   ðñ
õ #å ß *Ñ *å &å Ý Ý '÷7ñ 7ñ0 ˆWÓ÷zð zó ñzr   