
    Ih                        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y)z%Generic resource pool implementation.    )annotationsN)deque)Empty)	LifoQueue)TYPE_CHECKING   )
exceptions)register_after_fork)lazy)TracebackTypec                D    	 | j                          y # t        $ r Y y w xY wN)force_close_all	Exception)resources    G/var/www/html/planif/env/lib/python3.12/site-packages/kombu/resource.py_after_fork_cleanup_resourcer      s%      " s    	c                      e Zd ZdZd Zy)r   z#Last in first out version of Queue.c                "    t               | _        y r   )r   queue)selfmaxsizes     r   _initzLifoQueue._init   s    W
    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j0                  j3                  d      re
ZeZdZd Z
d Zyy)ResourcezPool of resources.FNc                    || _         |xs d| _        d| _        ||n| j                  | _        t	               | _        t               | _        | j                  rt        t        | t               | j                          y )Nr   F)_limitpreload_closedclose_after_forkr   	_resourceset_dirtyr
   r   setup)r   limitr$   r&   s       r   __init__zResource.__init__(   sp    |!  + 151F1F 	
 #e  %8%D&BC

r   c                    t        d      )Nzsubclass responsibility)NotImplementedErrorr   s    r   r*   zResource.setup7   s    !";<<r   c                    | j                   r=t        | j                        | j                   k\  r| j                  | j                         | j                  j                  | j                                y r   )r+   lenr)   LimitExceededr'   
put_nowaitnewr/   s    r   _add_when_emptyzResource._add_when_empty:   sL    ::#dkk*djj8$$TZZ00 	!!$((*-r   c                     j                   rt        d       j                  rM	 	  j                  j	                  ||      	  j                         j                  j                         n j                   j                                fd}|_
        S # t        $ r? t        t              r j                  j                           j                          w xY w# t        $ r  j                          Y nw xY w)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 pool)blocktimeoutc                 (    j                          y)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Or   )r%   RuntimeErrorr+   r'   getprepareBaseException
isinstancer   r3   r:   r)   addr   r5   r4   )r   r7   r8   r:   r;   s   `   @r   acquirezResource.acquireB   s     <<788::***HA	 LLO KKOOA&TXXZ(A		 	3 ) %a. NN55a8  !LLO  +((*+ s   C, B! !AC),DDc                    |S r   r   r   r   s     r   r>   zResource.preparev   s    r   c                $    |j                          y r   )closerD   s     r   close_resourcezResource.close_resourcey   s    r   c                     y r   r   rD   s     r   release_resourcezResource.release_resource|       r   c                t    | j                   r| j                  j                  |       | j                  |       y)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+    
 ::KK)H%r   c                    | j                   rH| j                  j                  |       | j                  j	                  |       | j                  |       y | j                  |       y r   )r+   r)   rL   r'   r3   rI   rG   rD   s     r   r:   zResource.release   sI    ::KK)NN%%h/!!(+)r   c                     y r   r   rD   s     r   collect_resourcezResource.collect_resource   rJ   r   c                ~   | j                   ry|| _         | j                  }| j                  }	 	 |j                         }	 | j                  |       $# t        $ r Y nw xY w# t        $ r Y w xY w	 	 |j                  j                         }n# t        $ r Y yw xY w	 | j                  |       n# t        $ r Y nw xY wN)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     <<!>>yy{%%d+    " nn((* %%c*!  sG   A  A! 	AA!	A-,A-2B 	BBB/ /	B;:B;c                f   | j                   }| j                  rCd|cxk  r| j                   k  r.n n+|s)|s%t        dj                  | j                   |            d}|| _         |r	 | j	                  d       | j                          ||k  r| j                  |dkD         y y # t
        $ r Y 7w xY w)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    [[
KKA33]"BIIU,- - E$$$6 	

:eai0   s   &B$ $	B0/B0c                    G d d      }| j                   }t        |d |             5  t        |j                        rt        |j                        t        | j                        z   | j
                  kD  r{|j                  j                         }|r| j                  |       t        |j                        r9t        |j                        t        | j                        z   | j
                  kD  r{d d d        y # 1 sw Y   y xY w)Nc                  *    e Zd Zd Z	 	 	 	 	 	 	 	 ddZy)#Resource._shrink_down.<locals>.Noopc                     y r   r   r/   s    r   	__enter__z-Resource._shrink_down.<locals>.Noop.__enter__   s    r   c                     y r   r   )r   exc_typeexc_valexc_tbs       r   __exit__z,Resource._shrink_down.<locals>.Noop.__exit__   s     r   N)ri   typerj   r   rk   r   returnNone)r   r   r   rg   rl   r   r   r   Noopre      s2     # &	
 r   rp   mutex)r'   getattrr1   r   r)   r+   popleftrP   )r   r[   rp   r   r;   s        r   r]   zResource._shrink_down   s    
	 
	 >>Xw/ 	-hnn%(3t{{+;;tzzINN**,))!,	 hnn%(3t{{+;;tzzI	- 	- 	-s   CC;;Dc                    | j                   S r   )r#   r/   s    r   r+   zResource.limit   s    {{r   c                &    | j                  |       y r   )rb   )r   r+   s     r   r+   zResource.limit   s    Er   KOMBU_DEBUG_POOLr   c                x   dd l }| j                  dz   x}| _        t        d| d| j                  j                           | j
                  |i |}||_        t        d| d| j                  j                          t        |d      sg |_        |j                  j                  |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'AbT4>>#:#:";<="""D3F3AANAbT4>>#:#:";<=1m, "MM  !7!7!9:Hr   c                    |j                   }t        d| d| j                  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   ss    &&BAbT4>>#:#:";<=""8,AAbT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h&*$L1$-.   \\  
zz~~()
		% *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      sF    + " 	   )    - "#
 a ar   