o
    Dfe                     @   s  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 zddlmZ W n ey?   dZY nw d	Zd
Zdag Zdd Zedd Zdd Zd'ddZd(ddZd)ddZd*ddZdd Zdd Zdd  Zd!d" Zd+d%d&ZdS ),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                 C   s   dd l }td|| )Nr   z!Blocking detection timed-out at: )inspectRuntimeErrorgetframeinfo)signumframer    r   K/home/ubuntu/webapp/venv/lib/python3.10/site-packages/celery/utils/debug.py_on_blocking"   s   r   c              	   c   sn    | sdV  dS t d }|tkrdn|}tt d< zt | V  W |r&|t d< t   dS |r2|t d< t   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_handlerr   r   r   r   )   s   

r   c                  C   s   t  } t|  | S )z\Sample RSS memory usage.

    Statistics can then be output by calling :func:`memdump`.
    )r   _mem_sampleappend)current_rssr   r   r   r	   ?   s   
r	   
   c                 C   sL   t }t|| krt|nt|| }g t d d < dd l}|  t }||fS )Nr   )r"   lenlistr   gccollectr   )samplesSprevr(   after_collectr   r   r   _memdumpI   s   r.   c                 C   sh   t t|d}t du r|d dS t| \}}|r*|d |D ]
}|d| d q|d| d dS )	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   r.   )r*   r0   sayr,   r-   memr   r   r   r
   S   s   
r
   c              	   c   sN    t | | }t|D ]}z| | V  W n ty   Y  dS w ||7 }qdS )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_r   r   r   r   f   s   
r      c                 C   s"   t | }|| kr
|S dj| |d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)fr?   ir   r   r   hfloatw   s   rD   c                    s   t  fddtD S )z4Convert bytes to human-readable form (e.g., KB, MB).c                 3   s8    | ]\}} |krt |r | n  | V  qd S N)rD   ).0divunitsr   r   	<genexpr>   s    zhumanbytes.<locals>.<genexpr>)nextUNITSrI   r   rI   r   r      s   r   c                  C   s    t  } | durtt| jS dS )z.Return RSS memory usage as a humanized string.N)r   r   _process_memory_inforssr>   r   r   r   r      s   r   c                   C   s    t du rtdurtt a t S )zReturn the global :class:`psutil.Process` instance.

    Note:
        Returns :const:`None` if :pypi:`psutil` is not installed.
    N)_processr   osgetpidr   r   r   r   r      s   r   c                 C   s&   z|   W S  ty   |   Y S w rE   )memory_infoAttributeErrorget_memory_info)processr   r   r   rN      s
   
rN   =1   c           
      C   s   ddl }| du rt n| } tt| d}dd | D }|| }t  D ]4\}}||}	|	s2q&||	j	  || t
j|| d || |d || t|j| d |d q&|  S )	zpReturn stack-trace of all active threads.

    See Also:
        Taken from https://gist.github.com/737056.
    r   Nr/   c                 S   s   i | ]}|j |qS r   )ident)rF   tr   r   r   
<dictcomp>   s    zcry.<locals>.<dictcomp>zLOCAL VARIABLES)stream
)	threadingr   r   r3   	enumeratesys_current_framesitemsgetname	tracebackprint_stackr   f_localsgetvalue)
outsepchrseplenr^   Ptmapseptidr   threadr   r   r   r      s$   

r   )r%   )r%   N)r   )r=   )NrW   rX   ) __doc__rQ   r`   re   
contextlibr   	functoolsr   r   celery.platformsr   celery.utils.textr   psutilr   ImportError__all__rM   rP   r"   r   r   r	   r.   r
   r   rD   r   r   r   rN   r   r   r   r   r   <module>   s>    






