
     h                         d 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
 Z G d de          Z G d d          ZdS )z%Generic resource pool implementation.    N)deque)Empty)	LifoQueue   )
exceptions)register_after_fork)lazyc                 R    	 |                                   d S # t          $ r Y d S w xY wN)force_close_all	Exception)resources    J/var/www/html/Sam_Eipo/venv/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 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.setup1   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_empty4   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 )av  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   r7   z!Resource.acquire.<locals>.release`   s     LLOOOOOr   )r"   RuntimeErrorr(   r$   getprepareBaseException
isinstancer	   r0   r7   r&   addr   r2   r1   )r   r4   r5   r7   r8   s   `   @r   acquirezResource.acquire<   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.preparem   s    r   c                 .    |                                  d S r   )closerA   s     r   close_resourcezResource.close_resourcep   s    r   c                     d S r   r   rA   s     r   release_resourcezResource.release_resources       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&   discardrD   rA   s     r   replacezResource.replacev   s>    
 : 	*K)))H%%%%%r   c                     | j         rK| j                            |           | j                            |           |                     |           d S |                     |           d S r   )r(   r&   rI   r$   r0   rF   rD   rA   s     r   r7   zResource.release   sl    : 	*K)))N%%h///!!(+++++)))))r   c                     d S r   r   rA   s     r   collect_resourcezResource.collect_resource   rG   r   c                    | j         rdS d| _         | 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)zClose and remove all resources in the pool (also those in use).

        Used to close resources from parent processes after fork
        (e.g. sockets/connections).
        NT)	r"   r&   r$   popKeyErrorrM   AttributeErrorr   
IndexError)r   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                 r   | j         }| j        rCd|cxk     r| j         k     r1n n.|s,|s(t          d                    | j         |                    d}|| _         |r&	 |                                  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={}T)collect)r    r&   r9   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 	$$&&&&   

:eai00000 s   A1 1
A>=A>Tc                 f    G d d          }| j         }t          |d |                      5  t          |j                  | j        k    rM|j                                        }|r|                     |           t          |j                  | j        k    Md d d            d S # 1 swxY w Y   d S )Nc                       e Zd Zd Zd ZdS )#Resource._shrink_down.<locals>.Noopc                     d S r   r   r,   s    r   	__enter__z-Resource._shrink_down.<locals>.Noop.__enter__       r   c                     d S r   r   )r   typevalue	tracebacks       r   __exit__z,Resource._shrink_down.<locals>.Noop.__exit__   rd   r   N)r   r   r   rc   ri   r   r   r   Noopra      s2              r   rj   mutex)r$   getattrr.   r   r(   popleftrM   )r   rW   rj   r   r8   s        r   rY   zResource._shrink_down   s   	 	 	 	 	 	 	 	 >Xw// 	- 	-hn%%
22N**,, -))!,,, hn%%
22	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-s   A+B&&B*-B*c                     | j         S r   )r    r,   s    r   r(   zResource.limit   s
    {r   c                 0    |                      |           d S r   )r^   )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)rh   _next_resource_idprint	__class__r   _orig_acquire_resource_idhasattrrt   appendformat_stack)r   argskwargsrh   idrs         r   r?   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 )Nrr   z	 RELEASE rs   r   )ry   rv   rw   r   _orig_releaseru   )r   r   r   r   s       r   r7   zResource.release   s~    &B<b<<4>#:<<===""8,,A<b<<4>#:<<===""a'""Hr   )NNN)FN)FFF)T)r   r   r   r   r   r/   r#   r)   r'   r2   r?   r;   rD   rF   rJ   r7   rM   r   r^   rY   propertyr(   setterosenvironr:   rx   r   ru   r   r   r   r   r      s       ,M   = = =. . ./ / / /b      & & &* * *    B1 1 1 1$- - - -    X \  \ 
z~~()) 
	 
	 
		 	 	 	 	% r   r   )r   r   collectionsr   r   r   r   
_LifoQueue r   utils.compatr   utils.functionalr	   r   r   r   r   r   <module>r      s    + + 				             ) ) ) ) ) )       - - - - - - " " " " " "      
   R R R R R R R R R Rr   