
    he                         d 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
 ddlmZ 	 ddlmZ d	Zd
Zdag Zd Zed        Zd ZddZddZddZddZd Zd Zd Zd ZddZy# e$ r dZY @w xY w)z:Utilities for debugging memory usage, blocking calls, etc.    N)contextmanager)partial)pprint)signals)
WhateverIO)Process)blockdetection
sample_memmemdumpsample
humanbytesmem_rsspscry))g      pBTB)g      AGB)g      0AMB)g      @KB)g        bc                 D    dd l }t        d|j                  |             )Nr   z!Blocking detection timed-out at: )inspectRuntimeErrorgetframeinfo)signumframer   s      N/var/www/Befach/backend/env/lib/python3.12/site-packages/celery/utils/debug.py_on_blockingr   "   s(    

+G,@,@,G+HI     c              #     K   | sd yt         d   }|t        k(  rdn|}t        t         d<   	 t        j                  |        |r	|t         d<   t        j                          y# |r	|t         d<   t        j                          w xY ww)zqContext that raises an exception if process is blocking.

    Uses ``SIGALRM`` to detect blocking functions.
    NALRM)r   r   	arm_alarmreset_alarm)timeoutold_handlers     r   r	   r	   )   sw      fo)\9d{&	"##G,,"-! "-!s   +B	A%  B	%!BB	c                  D    t               } t        j                  |        | S )z\Sample RSS memory usage.

    Statistics can then be output by calling :func:`memdump`.
    )r   _mem_sampleappend)current_rsss    r   r
   r
   ?   s    
 )K{#r   c                     t         }t        |      | k  rt        |      nt        ||       }g t         d d  dd l}|j                          t               }||fS )Nr   )r&   lenlistr   gccollectr   )samplesSprevr,   after_collects        r   _memdumpr2   I   sK    A!f'47VAw-?DKNJJLIMr   c                     t        t        |      }t               	 |d       yt        |       \  }}|r |d       |D ]  } |d| d         |d| d       y)	zDump memory statistics.

    Will print a sample of all RSS memory samples added by
    calling :func:`sample_mem`, and in addition print
    used RSS memory after :func:`gc.collect`.
    fileNz- rss: (psutil not installed).z- rss (sample):z-    > ,z- rss (end): .)r   printr   r2   )r.   r5   sayr0   r1   mems         r   r   r   S   sj     %d
#C	t|,-"7+D-C'#a ! -a()r   c              #      K   t        |       |z  }t        |      D ]  }	 | |    ||z  } y# t        $ r Y  yw xY ww)zGiven a list `x` a sample of length ``n`` of that list is returned.

    For example, if `n` is 10, and `x` has 100 items, a list of every tenth.
    item is returned.

    ``k`` can be used as offset.
    N)r*   range
IndexError)xnkj_s        r   r   r   f   sO      	A!A1X	A$J 	
Q   		s   ?/?	<?<?c                 L    t        |       }|| k(  r|S dj                  | |      S )zConvert float to value suitable for humans.

    Arguments:
        f (float): The floating point number.
        p (int): Floating point precision (default is 5).
    z{0:.{p}}p)intformat)frE   is      r   hfloatrJ   w   s.     	AAQ15J--a1-55r   c                 4     t         fdt        D              S )z4Convert bytes to human-readable form (e.g., KB, MB).c              3   Z   K   | ]"  \  }}|k\  rt        |r|z  n       |  $ y wN)rJ   ).0divunitss      r   	<genexpr>zhumanbytes.<locals>.<genexpr>   s:      C!s( S!c'a(
)$0s   (+)nextUNITS)rQ   s   `r   r   r      s       r   c                  X    t               } | t        t        |       j                        S y)z.Return RSS memory usage as a humanized string.N)r   r   _process_memory_inforssrD   s    r   r   r      s*    
A}.q15566 r   c                  `    t         #t        t        t        j                               a t         S )zReturn the global :class:`psutil.Process` instance.

    Note:
        Returns :const:`None` if :pypi:`psutil` is not installed.
    )_processr   osgetpid r   r   r   r      s$     G/299;'Or   c                 b    	 | j                         S # t        $ r | j                         cY S w xY wrM   )memory_infoAttributeErrorget_memory_info)processs    r   rV   rV      s4    )""$$ )&&(()s    ..c                    ddl }| 
t               n| } t        t        |       }|j	                         D ci c]  }|j
                  | }}||z  }t        j                         j                         D ]  \  }}	|j                  |      }
|
s ||
j                           ||       t        j                  |	|         ||        |d        ||       t        |	j                  |         |d        | j                         S c c}w )zpReturn stack-trace of all active threads.

    See Also:
        Taken from https://gist.github.com/737056.
    r   Nr4   zLOCAL VARIABLES)stream
)	threadingr   r   r8   	enumerateidentsys_current_framesitemsgetname	tracebackprint_stackr   f_localsgetvalue)outsepchrseplenre   Pttmapseptidr   threads              r   r   r      s     +*,3CC A !* 3 3 561AGGQJ6D6
6/C))+113
U#	V[[M	#e#.	#	
	#u~~c*	$ 4 <<>! 7s   D)
   )rz   N)r   )   )N=1   ) __doc__rZ   rh   rm   
contextlibr   	functoolsr   r   celery.platformsr   celery.utils.textr   psutilr   ImportError__all__rT   rY   r&   r   r	   r
   r2   r   r   rJ   r   r   r   rV   r   r\   r   r   <module>r      s    @ 	 
  %   $ (
	  " "**&"67	)m  Gs   A+ +A54A5