
    ^f                        d Z ddlmZ ddlZddlmZ ddlmZ ddlmZ	 ddl
mZ dd	lmZ dd
lmZ ddlmZ erddlmZ d Z G d de	          Z G d d          ZdS )z%Generic resource pool implementation.    )annotationsN)deque)Empty)	LifoQueue)TYPE_CHECKING   )
exceptions)register_after_fork)lazy)TracebackTypec                R    	 |                                   d S # t          $ r Y d S w xY wN)force_close_all	Exception)resources    @/var/www/html/env/lib/python3.11/site-packages/kombu/resource.py_after_fork_cleanup_resourcer      sA      """""   s    
&&c                      e Zd ZdZd ZdS )r   z#Last in first out version of Queue.c                ,    t                      | _        d S r   )r   queue)selfmaxsizes     r   _initzLifoQueue._init   s    WW


    N)__name__
__module____qualname____doc__r    r   r   r   r      s)        --    r   r   c                     e Zd ZdZej        ZdZddZd Zd Z	ddZ
d Zd	 Zd
 Zd Zd Zd ZddZddZddZed             Zej        d             Zej                            d          re
ZeZdZd Z
d ZdS dS )ResourcezPool of resources.FNc                   || _         |pd| _        d| _        ||n| j        | _        t	                      | _        t                      | _        | j        rt          t          | t                     | 
                                 d S )Nr   F)_limitpreload_closedclose_after_forkr   	_resourceset_dirtyr
   r   setup)r   limitr$   r&   s       r   __init__zResource.__init__(   s    |!  + 151F 	
 #ee  	D%8%D&BCCC

r   c                     t          d          )Nzsubclass responsibility)NotImplementedErrorr   s    r   r*   zResource.setup7   s    !";<<<r   c                    | j         r7t          | j                  | j         k    r|                     | j                   | j                            |                                            d S r   )r+   lenr)   LimitExceededr'   
put_nowaitnewr/   s    r   _add_when_emptyzResource._add_when_empty:   s^    : 	1#dk**dj88$$TZ000 	!!$((**-----r   c                @     j         rt          d           j        r	 	  j                            ||          	                                nT# t          $ rG t          t                    r j        	                               n 
                                w xY w j                                       n%# t          $ r                                   Y nw xY wn'                                                                 fd}|_
        S )a  Acquire resource.

        Arguments:
        ---------
            block (bool): If the limit is exceeded,
                then block until there is an available item.
            timeout (float): Timeout to wait
                if ``block`` is true.  Default is :const:`None` (forever).

        Raises
        ------
            LimitExceeded: if block is false and the limit has been exceeded.
        zAcquire on closed poolr   )blocktimeoutc                 2                                     dS )a'  Release resource so it can be used by another thread.

            Warnings:
            --------
                The caller is responsible for discarding the object,
                and to never use the resource again.  A new resource must
                be acquired if so needed.
            N)release)Rr   s   r   r:   z!Resource.acquire.<locals>.releaseh   s     LLOOOOOr   )r%   RuntimeErrorr+   r'   getprepareBaseException
isinstancer   r3   r:   r)   addr   r5   r4   )r   r7   r8   r:   r;   s   `   @r   acquirezResource.acquireB   sZ    < 	97888: 	)***HHA	 LLOO(   %a.. , N55a8888 !LLOOO KOOA&&&  + + +((*****+" TXXZZ((A		 		 		 		 		 		 	s   C A AB&C%$C%c                    |S r   r   r   r   s     r   r>   zResource.preparev   s    r   c                .    |                                  d S r   )closerD   s     r   close_resourcezResource.close_resourcey   s    r   c                    d S r   r   rD   s     r   release_resourcezResource.release_resource|       r   c                r    | j         r| j                            |           |                     |           dS )zqReplace existing resource with a new instance.

        This can be used in case of defective resources.
        N)r+   r)   discardrG   rD   s     r   replacezResource.replace   s>    
 : 	*K)))H%%%%%r   c                    | j         rK| j                            |           | j                            |           |                     |           d S |                     |           d S r   )r+   r)   rL   r'   r3   rI   rG   rD   s     r   r:   zResource.release   sl    : 	*K)))N%%h///!!(+++++)))))r   c                    d S r   r   rD   s     r   collect_resourcezResource.collect_resource   rJ   r   Tc                   | j         rdS || _         | j        }| j        }	 	 |                                }n# t          $ r Y n,w xY w	 |                     |           n# t          $ r Y nw xY wN	 	 |j                                        }n# t          $ r Y dS w xY w	 |                     |           n# t          $ r Y nw xY wT)aa  Close and remove all resources in the pool (also those in use).

        Used to close resources from parent processes after fork
        (e.g. sockets/connections).

        Arguments:
        ---------
            close_pool (bool): If True (default) then the pool is marked
                as closed. In case of False the pool can be reused.
        N)	r%   r)   r'   popKeyErrorrP   AttributeErrorr   
IndexError)r   
close_pooldirtyr   dresress         r   r   zResource.force_close_all   s$    < 	F!>	yy{{   %%d++++!   		n((**   %%c****!   	sE   6 
AAA 
A*)A*0B
 

BBB2 2
B?>B?c                v   | j         }| j        rCd|cxk     r| j         k     r1n n.|s,|s(t          d                    | j         |                    d}|| _         |r(	 |                     d           n# t
          $ r Y nw xY w|                                  ||k     r|                     |dk               d S d S )Nr   z,Can't shrink pool when in use: was={} now={}TF)rV   )collect)r#   r)   r<   formatr   r   r*   _shrink_down)r   r+   forceignore_errorsreset
prev_limits         r   resizezResource.resize   s   [
K 	A333333333]3 -"BIIU, ,- - - E 	$$$6666   

:eai00000 s   A3 3
B ?B c                
    G d d          }| j         }t          |d |                      5  t          |j                  rt          |j                  t          | j                  z   | j        k    rv|j                                        }|r|                     |           t          |j                  r2t          |j                  t          | j                  z   | j        k    vd d d            d S # 1 swxY w Y   d S )Nc                      e Zd Zd Zdd
ZdS )#Resource._shrink_down.<locals>.Noopc                    d S r   r   r/   s    r   	__enter__z-Resource._shrink_down.<locals>.Noop.__enter__   s    r   exc_typetypeexc_valr   exc_tbr   returnNonec                    d S r   r   )r   rh   rj   rk   s       r   __exit__z,Resource._shrink_down.<locals>.Noop.__exit__   s	     r   N)rh   ri   rj   r   rk   r   rl   rm   )r   r   r   rg   ro   r   r   r   Noopre      s7               r   rp   mutex)r'   getattrr1   r   r)   r+   popleftrP   )r   r[   rp   r   r;   s        r   r]   zResource._shrink_down   sP   
	 
	 
	 
	 
	 
	 
	 
	 >Xw// 	- 	-hn%% -((3t{+;+;;tzIIN**,, -))!,,,	 hn%% -((3t{+;+;;tzII	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-s   B=C88C<?C<c                    | j         S r   )r#   r/   s    r   r+   zResource.limit   s
    {r   c                0    |                      |           d S r   )rb   )r   r+   s     r   r+   zResource.limit   s    Er   KOMBU_DEBUG_POOLr   c                Z   dd l }| j        dz   x}| _        t          d| d| j        j                     | j        |i |}||_        t          d| d| j        j                    t          |d          sg |_        |j        	                    |
                                           |S )Nr   r   +z	 ACQUIRE -acquired_by)	traceback_next_resource_idprint	__class__r   _orig_acquire_resource_idhasattrrz   appendformat_stack)r   argskwargsr{   idrs         r   rB   zResource.acquire   s    *.*@1*DDB'<b<<4>#:<<===""D3F33AAN<b<<4>#:<<===1m,, # "M  !7!7!9!9:::Hr   c                    |j         }t          d| d| j        j                    |                     |          }t          d| d| j        j                    | xj        dz  c_        |S )Nrx   z	 RELEASE ry   r   )r   r}   r~   r   _orig_releaser|   )r   r   r   r   s       r   r:   zResource.release   s~    &B<b<<4>#:<<===""8,,A<b<<4>#:<<===""a'""Hr   )NNN)FN)T)FFF)r   r   r   r   r	   r2   r&   r,   r*   r5   rB   r>   rG   rI   rM   r:   rP   r   rb   r]   propertyr+   setterosenvironr=   r   r   r|   r   r   r   r!   r!   !   s       ,M   = = =. . .2 2 2 2h      & & &* * *  $ $ $ $L1 1 1 1$- - - -.   X \  \ 
z~~()) 
	 
	 
		 	 	 	 	% r   r!   )r   
__future__r   r   collectionsr   r   r   r   
_LifoQueuetypingr    r	   utils.compatr
   utils.functionalr   typesr   r   r!   r   r   r   <module>r      s3   + + " " " " " " 				             ) ) ) ) ) )                   - - - - - - " " " " " " $######      
   a a a a a a a a a ar   