o
    Òó·i  ã                   @  sˆ   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„ dƒZe
eƒG d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                   @  s   e Zd ZdZd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                 C  s   || _ || _|| _d S ©N)r   r   r   )Úselfr   r   r   © r   ú\C:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/_threads/_team.pyÚ__init__%   s   
zStatistics.__init__N)r   r   r   r   r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r      s    r   c                   @  sn   e Zd ZdZd&dd	„Zd'dd„Zd(dd„Zd)d*dd„Zd)d*dd„Zd+dd„Z	d,dd„Z
d-d"d#„Zd.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                 C  s@   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    r   r   r   r   M   s   
zTeam.__init__r   r   c                 C  s   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}.
        N)r   Úlenr'   r(   r)   ©r   r   r   r   Ú
statisticsm   s   zTeam.statisticsÚnr   r   c                   s&   ˆj  ¡  ˆjjd‡ ‡fd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                    s2   t ˆ ƒD ]} ˆ ¡ }|d u r d S ˆ |¡ qd S r   )Úranger$   Ú_recycleWorker)ÚxÚworker©r/   r   r   r   ÚcreateOneWorker~   s   üz"Team.grow.<locals>.createOneWorkerN©r   r   ©r"   Úcheckr#   Údo)r   r/   r5   r   r4   r   Úgrowu   s   
z	Team.growNúOptional[int]c                   s$   ˆj  ¡  ˆj ‡ ‡fd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
   ˆ  ˆ ¡S r   )Ú_quitIdlersr   r4   r   r   Ú<lambda>�   ó   
 zTeam.shrink.<locals>.<lambda>Nr7   )r   r/   r   r4   r   Úshrink†   s   
zTeam.shrinkc                 C  sp   |du rt | jƒ| j }t|ƒD ]}| jr| j ¡  ¡  q|  jd7  _q| jr4| jdkr6| 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   )	r,   r'   r(   r0   ÚpopÚquitr+   r*   r#   )r   r/   r2   r   r   r   r<   ‘   s   ÿzTeam._quitIdlersÚtaskúCallable[[], object]c                   s$   ˆ j  ¡  ˆ j ‡ ‡fdd„¡ dS )zu
        Perform some work in a worker created by C{createWorker}.

        @param task: the callable to run
        c                     s
   ˆ   ˆ¡S r   )Ú_coordinateThisTaskr   ©r   rB   r   r   r=   ¨   r>   zTeam.do.<locals>.<lambda>Nr7   rE   r   rE   r   r9   ¡   s   
zTeam.doúCallable[..., object]c                   s^   ˆj rˆj  ¡ nˆ ¡ }|du rˆj ˆ¡ dS |‰ ˆ jd7  _|jd‡ ‡‡fd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                    sB   zˆƒ  W n t y   ˆ ¡  Y nw ˆjjd‡ ‡fdd„ƒ} d S )Nr   r   c                     s   ˆ j d8  _ ˆ ˆ ¡ d S )Nr   )r(   r1   r   )Únot_none_workerr   r   r   ÚidleAndPendingÄ   s   z@Team._coordinateThisTask.<locals>.doWork.<locals>.idleAndPendingr6   )ÚBaseExceptionr%   r#   r9   )rH   ©rG   r   rB   r   r   ÚdoWork½   s   
ÿz(Team._coordinateThisTask.<locals>.doWorkr6   )r'   r@   r$   r)   Úappendr(   r9   )r   rB   r3   rK   r   rJ   r   rD   ª   s   
zTeam._coordinateThisTaskr3   r	   c                 C  sl   | j  |¡ | jr|  | j ¡ ¡ dS | jr|  ¡  dS | jdkr4|  jd8  _| 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)   rD   Úpopleftr*   r<   r+   ÚremoverA   )r   r3   r   r   r   r1   É   s   	
ýzTeam._recycleWorkerc                   s$   ˆ j  ¡  ˆ jjd‡ fdd„ƒ}dS )zA
        Stop doing work and shut down all idle workers.
        r   r   c                     s   dˆ _ ˆ  ¡  d S )NT)r*   r<   r   r-   r   r   ÚstartFinishingå   s   z!Team.quit.<locals>.startFinishingNr6   )r"   r&   r#   r9   )r   rP   r   r-   r   rA   Þ   s   
z	Team.quit)r   r   r   r   r    r!   )r   r   )r/   r   r   r   r   )r/   r;   r   r   )rB   rC   r   r   )rB   rF   r   r   )r3   r	   r   r   r6   )r   r   r   r   r   r.   r:   r?   r<   r9   rD   r1   rA   r   r   r   r   r   -   s    

 


	
r   N)r   Ú
__future__r   Úcollectionsr   Útypingr   r   r   Zzope.interfacer   Ú r	   Z_conveniencer
   Z	_ithreadsr   r   r   r   r   r   r   Ú<module>   s   