
    `fP%                        d Z ddlZddlZddlZddl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 nJ# e$ rB 	 ddlmZ n7# e$ r/ 	 ddlmZ n$# e$ r 	 ddlmZ n# e$ r	 ddlmZ Y nw xY wY nw xY wY nw xY wY nw xY wdZej                            d	          Zed
             Z G d dej                  Zd Z G d d          Z G d d          Z G d d          Z G d dej                  Z ere Z!dS eZ!dS )z#Threading primitives and utilities.    N)contextmanager)TIMEOUT_MAXProxy)
getcurrent)	get_ident)bgThreadLocal
LocalStackLocalManagerr   default_socket_timeoutUSE_FAST_LOCALSc              #      K   t          j                    }t          j        |            dV  t          j        |           dS )z7Context temporarily setting the default socket timeout.N)socketgetdefaulttimeoutsetdefaulttimeout)timeoutprevs     F/var/www/html/env/lib/python3.11/site-packages/celery/utils/threads.pyr   r   #   sG       #%%D
W%%%	EEE
T"""""    c                   B     e Zd ZdZd	 fd	Zd Zd Zd Zd Zd Z	 xZ
S )
r	   zBackground service thread.Nc                     t                                                       t          j                    | _        t          j                    | _        d| _        |p| j        j        | _	        d S )NT)
super__init__	threadingEvent_bgThread__is_shutdown_bgThread__is_stoppeddaemon	__class____name__name)selfr"   kwargsr    s      r   r   zbgThread.__init__/   sU    &_..%O--3DN3			r   c                     t                      N)NotImplementedErrorr#   s    r   bodyzbgThread.body6   s    !###r   c                     t           |j        | t          j                   t	          j        d t          j                   d S )N)file)printformatsysstderr	traceback	print_exc)r#   msgfmtr$   s       r   on_crashzbgThread.on_crash9   s;    jcj#SZ0000D#*-----r   c                 ,   | j         }| j        j        }	  |            s	  |             n# t          $ r}	 |                     d| j        |           |                                  t          j        	                                 t          j        d           n7# t          j        	                                 t          j        d           w xY wY d }~nd }~ww xY w |            |                                  d S # |                                  w xY w)Nz{0!r} crashed: {1!r}   )r)   r   is_set	Exceptionr4   r"   _set_stoppedr.   r/   flushos_exit)r#   r)   shutdown_setexcs       r   runzbgThread.run=   s    y)0	 "lnn 	$$DFFFF  $ $ $$&<diMMM))+++
((*** 
((***$ #lnn 	$ DsB   
C= 
+ C= 
C0B&3C4CCC= CC= =Dc                 \    	 | j                                          d S # t          $ r Y d S w xY wr&   )r   set	TypeErrorr(   s    r   r9   zbgThread._set_stoppedN   sF    	!!##### 	 	 	 DD	s    
++c                     | j                                          | j                                         |                                 r|                     t                     dS dS )zGraceful shutdown.N)r   rA   r   waitis_alivejoinTHREAD_TIMEOUT_MAXr(   s    r   stopzbgThread.stopV   s_          ==?? 	*II()))))	* 	*r   r&   )r!   
__module____qualname____doc__r   r)   r4   r?   r9   rH   __classcell__r    s   @r   r	   r	   ,   s        $$4 4 4 4 4 4$ $ $. . .     "  * * * * * * *r   r	   c                 .    |                                   dS )a-  Release the contents of the local for the current context.

    This makes it possible to use locals without a manager.

    With this function one can release :class:`Local` objects as well as
    :class:`StackLocal` objects.  However it's not possible to
    release data held by proxies that way, one always has to retain
    a reference to the underlying local object in order to be able
    to release it.

    Example:
        >>> loc = Local()
        >>> loc.foo = 42
        >>> release_local(loc)
        >>> hasattr(loc, 'foo')
        False
    N)__release_local__)locals    r   release_localrQ   ^   s    $ 
r   c                   @    e Zd ZdZdZd Zd Zd Zd Zd Z	d Z
d	 Zd
S )r
   zLocal object.)__storage____ident_func__c                     t                               | di            t                               | dt                     d S )NrS   rT   )object__setattr__r   r(   s    r   r   zLocal.__init__x   s9    43334!19=====r   c                 N    t          | j                                                  S r&   )iterrS   itemsr(   s    r   __iter__zLocal.__iter__|   s    D$**,,---r   c                 "    t          | |          S )zCreate a proxy for a name.r   )r#   proxys     r   __call__zLocal.__call__   s    T5!!!r   c                 `    | j                             |                                 d            d S r&   )rS   poprT   r(   s    r   rO   zLocal.__release_local__   s-    T0022D99999r   c                     	 | j         |                                          |         S # t          $ r t          |          w xY wr&   rS   rT   KeyErrorAttributeErrorr#   r"   s     r   __getattr__zLocal.__getattr__   sO    	'#D$7$7$9$9:4@@ 	' 	' 	' &&&	's	   $' Ac                     |                                  }| j        }	 |||         |<   d S # t          $ r ||i||<   Y d S w xY wr&   )rT   rS   rc   )r#   r"   valueidentstorages        r   rW   zLocal.__setattr__   sb    ##%%"	+#(GEN4    	+ 	+ 	+"E]GENNNN	+s   * ??c                     	 | j         |                                          |= d S # t          $ r t          |          w xY wr&   rb   re   s     r   __delattr__zLocal.__delattr__   sR    	' !4!4!6!67=== 	' 	' 	' &&&	's    $ >N)r!   rI   rJ   rK   	__slots__r   r[   r^   rO   rf   rW   rl    r   r   r
   r
   s   s        1I> > >. . ." " ": : :' ' '+ + +' ' ' ' 'r   r
   c                       e Zd ZdZd Zd Zd Zd Z eee          Z	[[d Z
d Zd Zd	 Zed
             Zed             ZdS )_LocalStacka  Local stack.

    This class works similar to a :class:`Local` but keeps a stack
    of objects instead.  This is best explained with an example::

        >>> ls = LocalStack()
        >>> ls.push(42)
        >>> ls.top
        42
        >>> ls.push(23)
        >>> ls.top
        23
        >>> ls.pop()
        23
        >>> ls.top
        42

    They can be force released by using a :class:`LocalManager` or with
    the :func:`release_local` function but the correct way is to pop the
    item from the stack after using.  When the stack is empty it will
    no longer be bound to the current context (and as such released).

    By calling the stack without arguments it will return a proxy that
    resolves to the topmost item on the stack.
    c                 ,    t                      | _        d S r&   )r
   _localr(   s    r   r   z_LocalStack.__init__   s    ggr   c                 8    | j                                          d S r&   )rr   rO   r(   s    r   rO   z_LocalStack.__release_local__   s    %%'''''r   c                     | j         j        S r&   )rr   rT   r(   s    r   _get__ident_func__z_LocalStack._get__ident_func__   s    {))r   c                 H    t                               | j        d|           d S NrT   )rV   rW   rr   )r#   rh   s     r   _set__ident_func__z_LocalStack._set__ident_func__   s#    4;(8%@@@@@r   c                 ,      fd}t          |          S )Nc                  8    j         } | t          d          | S )Nzobject unbound)topRuntimeError)rvr#   s    r   _lookupz%_LocalStack.__call__.<locals>._lookup   s#    Bz"#3444Ir   r   )r#   r~   s   ` r   r^   z_LocalStack.__call__   s*    	 	 	 	 	
 W~~r   c                 |    t          | j        dd          }|g x| j        _        }|                    |           |S )zPush a new item to the stack.stackN)getattrrr   r   append)r#   objr}   s      r   pushz_LocalStack.push   s?    T['400: &('DK
		#	r   c                     t          | j        dd          }|dS t          |          dk    rt          | j                   |d         S |                                S )zRemove the topmost item from the stack.

        Note:
            Will return the old value or `None` if the stack was already empty.
        r   Nr6   )r   rr   lenrQ   r`   r#   r   s     r   r`   z_LocalStack.pop   sX     Wd33=4ZZ1__$+&&&999;;r   c                 T    t          | j        dd           }|rt          |          ndS )Nr   r   )r   rr   r   r   s     r   __len__z_LocalStack.__len__   s*    Wd33")s5zzz)r   c                 :    t          | j        dd           }||S g S )Nr   )r   rr   r   s     r   r   z_LocalStack.stack   s'     Wd33L	r   c                 X    	 | j         j        d         S # t          t          f$ r Y dS w xY w)ztThe topmost item on the stack.

        Note:
            If the stack is empty, :const:`None` is returned.
        r   N)rr   r   rd   
IndexErrorr(   s    r   r{   z_LocalStack.top   s>    	;$R((
+ 	 	 	44	s    ))N)r!   rI   rJ   rK   r   rO   ru   rx   propertyrT   r^   r   r`   r   r   r{   rn   r   r   rp   rp      s         4  ( ( (* * *A A AX02DEEN.      * * *   X 	 	 X	 	 	r   rp   c                   ,    e Zd ZdZddZd Zd Zd ZdS )r   a  Local objects cannot manage themselves.

    For that you need a local manager.
    You can pass a local manager multiple locals or add them
    later by appending them to ``manager.locals``.  Every time the manager
    cleans up, it will clean up all the data left in the locals for this
    context.

    The ``ident_func`` parameter can be added to override the default ident
    function for the wrapped locals.
    Nc                     |g | _         n2t          |t                    r	|g| _         nt          |          | _         |/|| _        | j         D ]}t
                              |d|           d S t          | _        d S rw   )locals
isinstancer
   list
ident_funcrV   rW   r   )r#   r   r   rP   s       r   r   zLocalManager.__init__  s    >DKK&& 	'!(DKKv,,DK!(DO H H""5*:JGGGGH H (DOOOr   c                 *    |                                  S )a4  Return context identifier.

        This is the identifier the local objects use internally
        for this context.  You cannot override this method to change the
        behavior but use it to link other context local objects (such as
        SQLAlchemy's scoped sessions) to the Werkzeug locals.
        )r   r(   s    r   r   zLocalManager.get_ident  s        r   c                 8    | j         D ]}t          |           dS )zManually clean up the data in the locals for this context.

        Call this at the end of the request or use ``make_middleware()``.
        N)r   rQ   )r#   rP   s     r   cleanupzLocalManager.cleanup$  s0    
 [ 	! 	!E%    	! 	!r   c                 f    d                     | j        j        t          | j                            S )Nz<{} storages: {}>)r-   r    r!   r   r   r(   s    r   __repr__zLocalManager.__repr__,  s.    "))N#S%5%57 7 	7r   )NN)r!   rI   rJ   rK   r   r   r   r   rn   r   r   r   r      s_        
 
( ( ( (! ! !! ! !7 7 7 7 7r   r   c                   :     e Zd Z fdZed             Zd Z xZS )_FastLocalStackc                     g | _         | j         j        | _        | j         j        | _        t	                                                       d S r&   )r   r   r   r`   r   r   )r#   r    s    r   r   z_FastLocalStack.__init__3  s=    
J%	:>r   c                 N    	 | j         d         S # t          t          f$ r Y d S w xY w)Nr   )r   rd   r   r(   s    r   r{   z_FastLocalStack.top9  s:    	:b>!
+ 	 	 	44	s    $$c                 *    t          | j                  S r&   )r   r   r(   s    r   r   z_FastLocalStack.__len__@  s    4:r   )r!   rI   rJ   r   r   r{   r   rL   rM   s   @r   r   r   1  sb               X      r   r   )"rK   r;   r   r.   r   r0   
contextlibr   r   rG   celery.localr   greenletr   r   ImportError_threadthread_dummy_threaddummy_thread__all__environgetr   r   Threadr	   rQ   r
   rp   r   rP   r   r   rn   r   r   <module>r      s   ) ) 				  



         % % % % % % 7 7 7 7 7 7      30000000 
3 
3 
3	3%%%%%%% 3 3 3	3((((((( 	3 	3 	333333333 3 3 3222222223	33
3
 *..!233 # # #/* /* /* /* /*y /* /* /*d  *%' %' %' %' %' %' %' %'Pa a a a a a a aH/7 /7 /7 /7 /7 /7 /7 /7d    io   &   JJJ JJJsw   1 A8>A8A2A
A2A,AA,A&	#A,%A&	&A,)A2+A,,A2/A81A22A87A8