
    leJi                    ~    d Z ddlmZ ddlZddlmZ ddlmZmZ ddl	m
Z
 ddlmZ dd	lmZ d
 Z G d d          ZdS )z%Generic resource pool implementation.    )annotationsN)nullcontext)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    T/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/kombu/resource.py_after_fork_cleanup_resourcer      sA      """""   s    
&&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)selflimitr   r   s       r   __init__zResource.__init__   s    |!  + 151F 	
 #ee  	D%8%D&BCCC

    c                     t          d          )Nzsubclass responsibility)NotImplementedErrorr   s    r   r   zResource.setup+   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>.release\   s     LLOOOOOr    )r   RuntimeErrorr   r   getprepareBaseException
isinstancer
   r'   r.   r   addr   r)   r(   )r   r+   r,   r.   r/   s   `   @r   acquirezResource.acquire6   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   s     r   r2   zResource.preparej   s    r    c                .    |                                  d S r   )closer9   s     r   close_resourcezResource.close_resourcem   s    r    c                    d S r   r8   r9   s     r   release_resourcezResource.release_resourcep       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   discardr<   r9   s     r   replacezResource.replaces   s>    
 : 	*K)))H%%%%%r    c                    | j         rK| j                            |           | j                            |           |                     |           d S |                     |           d S r   )r   r   rA   r   r'   r>   r<   r9   s     r   r.   zResource.release|   sl    : 	*K)))N%%h///!!(+++++)))))r    c                    d S r   r8   r9   s     r   collect_resourcezResource.collect_resource   r?   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KeyErrorrE   AttributeErrorqueue
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)rL   )collect)r   r   r0   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                   | j         }t          |dt                                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 )Nmutex)	r   getattrr   r%   rJ   r   r   rG   rE   )r   rQ   r   r/   s       r   rS   zResource._shrink_down   s(   > Xw66 	- 	-hn%% -3x~+>+>T[AQAQ+QUYU_*_*_N&&(( -))!,,, hn%% -3x~+>+>T[AQAQ+QUYU_*_*_	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-s   B=C//C36C3c                    | j         S r   )r   r#   s    r   r   zResource.limit   s
    {r    c                0    |                      |           d S r   )rX   )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____name___orig_acquire_resource_idhasattrrb   appendformat_stack)r   argskwargsrc   idrs         r   r6   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 )Nr`   z	 RELEASE ra   r   )ri   re   rf   rg   _orig_releaserd   )r   r   ro   rp   s       r   r.   zResource.release   s~    &B<b<<4>#:<<===""8,,A<b<<4>#:<<===""a'""Hr    )NNN)FN)T)FFF)rg   
__module____qualname____doc__r   r&   r   r   r   r)   r6   r2   r<   r>   rB   r.   rE   r   rX   rS   propertyr   setterosenvironr1   rh   rr   rd   r8   r    r   r   r      s       ,M   = = =. . .2 2 2 2h      & & &* * *  ! ! ! !F1 1 1 1$	- 	- 	- 	-   X \  \ 
z~~()) 
	 
	 
		 	 	 	 	% r    r   )ru   
__future__r   rx   
contextlibr   rJ   r   r    r   utils.compatr	   utils.functionalr
   r   r   r8   r    r   <module>r      s    + + " " " " " " 				 " " " " " " " " " " " " " "       - - - - - - " " " " " "  R R R R R R R R R Rr    