
    id                     ~    d dl Z d dlmZ d dlmZ ddgZ G d d          Z G d d          Z G d	 de          ZdS )
    N)contextmanager)queuePool	TokenPoolc                   T    e Zd ZdZddZd Zed             Zd	 Zd
 Z	d Z
d Zd ZdS )r   a  
    Pool class implements resource limitation and construction.

    There are two ways of using Pool: passing a `create` argument or
    subclassing. In either case you must provide a way to create
    the resource.

    When using `create` argument, pass a function with no arguments::

        http_pool = pools.Pool(create=httplib2.Http)

    If you need to pass arguments, build a nullary function with either
    `lambda` expression::

        http_pool = pools.Pool(create=lambda: httplib2.Http(timeout=90))

    or :func:`functools.partial`::

        from functools import partial
        http_pool = pools.Pool(create=partial(httplib2.Http, timeout=90))

    When subclassing, define only the :meth:`create` method
    to implement the desired resource::

        class MyPool(pools.Pool):
            def create(self):
                return MyObject()

    If using 2.5 or greater, the :meth:`item` method acts as a context manager;
    that's the best way to use it::

        with mypool.item() as thing:
            thing.dostuff()

    The maximum size of the pool can be modified at runtime via
    the :meth:`resize` method.

    Specifying a non-zero *min-size* argument pre-populates the pool with
    *min_size* items.  *max-size* sets a hard limit to the size of the pool --
    it cannot contain any more items than *max_size*, and if there are already
    *max_size* items 'checked out' of the pool, the pool will cause any
    greenthread calling :meth:`get` to cooperatively yield until an item
    is :meth:`put` in.
    r      FNc                 N   || _         || _        || _        d| _        t	          j        d          | _        t          j                    | _	        ||| _
        t          |          D ]>}| xj        dz  c_        | j	                            | 
                                           ?dS )a  *order_as_stack* governs the ordering of the items in the free pool.
        If ``False`` (the default), the free items collection (of items that
        were created and were put back in the pool) acts as a round-robin,
        giving each item approximately equal utilization.  If ``True``, the
        free pool acts as a FILO stack, which preferentially re-uses items that
        have most recently been used.
        r   N   )min_sizemax_sizeorder_as_stackcurrent_sizer   
LightQueuechannelcollectionsdeque
free_itemscreaterangeappend)selfr   r   r   r   xs         9/usr/local/lib/python3.11/dist-packages/eventlet/pools.py__init__zPool.__init__8   s     ! ,'**%+-- DKx 	2 	2A"O""4;;==1111	2 	2    c                 0   | j         r| j                                         S | xj        dz  c_        | j        | j        k    r.	 |                                 }n#  | xj        dz  c_         xY w|S | xj        dz  c_        | j                                        S )zwReturn an item from the pool, when one is available.  This may
        cause the calling greenthread to block.
        r
   )r   popleftr   r   r   r   get)r   createds     r   r   zPool.getM   s     ? 	-?**,,,Q--++--!!Q&!!NQ|!!!s   A A*c              #      K   |                                  }	 |V  |                     |           dS # |                     |           w xY w)a$   Get an object out of the pool, for use with with statement.

        >>> from eventlet import pools
        >>> pool = pools.TokenPool(max_size=4)
        >>> with pool.item() as obj:
        ...     print("got token")
        ...
        got token
        >>> pool.free()
        4
        N)r   put)r   objs     r   itemz	Pool.item^   sJ       hhjj	IIIHHSMMMMMDHHSMMMMs	   3 A
c                 T   | j         | j        k    r| xj         dz  c_         dS |                                 r4	 | j                            |d           dS # t
          j        $ r Y nw xY w| j        r| j        	                    |           dS | j        
                    |           dS )zmPut an item back into the pool, when done.  This may
        cause the putting greenthread to block.
        r
   NF)block)r   r   waitingr   r!   r   Fullr   r   
appendleftr   )r   r#   s     r   r!   zPool.putq   s     t},,"F<<>> 	  U 333:     	)O&&t,,,,,O""4(((((s   A A('A(c                     || _         dS )ay  Resize the pool to *new_size*.

        Adjusting this number does not affect existing items checked out of
        the pool, nor on any greenthreads who are waiting for an item to free
        up.  Some indeterminate number of :meth:`get`/:meth:`put`
        cycles will be necessary before the new maximum size truly matches
        the actual operation of the pool.
        N)r   )r   new_sizes     r   resizezPool.resize   s     !r   c                 J    t          | j                  | j        z   | j        z
  S )zReturn the number of free items in the pool.  This corresponds
        to the number of :meth:`get` calls needed to empty the pool.
        )lenr   r   r   r   s    r   freez	Pool.free   s#     4?##dm3d6GGGr   c                     t          d| j                                        | j                                        z
            S )z?Return the number of routines waiting for a pool item.
        r   )maxr   gettingputtingr.   s    r   r&   zPool.waiting   s5     1dl**,,t|/C/C/E/EEFFFr   c                      t          d          )a%  Generate a new pool item.  In order for the pool to
        function, either this method must be overriden in a subclass
        or the pool must be constructed with the `create` argument.
        It accepts no arguments and returns a single instance of
        whatever thing the pool is supposed to contain.

        In general, :meth:`create` is called whenever the pool exceeds its
        previous high-water mark of concurrently-checked-out-items.  In other
        words, in a new pool with *min_size* of 0, the very first call
        to :meth:`get` will result in a call to :meth:`create`.  If the first
        caller calls :meth:`put` before some other caller calls :meth:`get`,
        then the first item will be returned, and :meth:`create` will not be
        called a second time.
        zImplement in subclass)NotImplementedErrorr.   s    r   r   zPool.create   s     ""9:::r   )r   r   FN)__name__
__module____qualname____doc__r   r   r   r#   r!   r+   r/   r&   r    r   r   r   r   
   s        + +Z2 2 2 2*" " ""   ^$) ) )(	! 	! 	!H H HG G G
; ; ; ; ;r   c                       e Zd ZdS )TokenN)r6   r7   r8   r:   r   r   r<   r<      s        Dr   r<   c                       e Zd ZdZd ZdS )r   zA pool which gives out tokens (opaque unique objects), which indicate
    that the coroutine which holds the token has a right to consume some
    limited resource.
    c                     t                      S )N)r<   r.   s    r   r   zTokenPool.create   s    wwr   N)r6   r7   r8   r9   r   r:   r   r   r   r      s-         
    r   )	r   
contextlibr   eventletr   __all__r   r<   r   r:   r   r   <module>rB      s        % % % % % %       ;
`; `; `; `; `; `; `; `;F	 	 	 	 	 	 	 	         r   