o
    wvXj  ã                   @   s  d Z ddlmZmZm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mZmZ ddlmZ zdd	lmZ W n eyM   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.é    )Úabsolute_importÚprint_functionÚunicode_literalsN)Úcontextmanager)Úpartial)Úpprint)Ú
WhateverIOÚitemsÚrange)Úsignals)Ú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: {0})ÚinspectÚRuntimeErrorÚformatÚgetframeinfo)ÚsignumÚframer   © r    úO/var/www/html/myproject/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   D   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!   Ú_memdumpN   s   r4   c                 C   sd   t t|d�}tƒ du r|dƒ dS t| ƒ\}}|r)|dƒ |D ]	}|d |¡ƒ q|d |¡ƒ dS )z¼Dump 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-    > {0},z- rss (end): {0}.)r   Úprintr   r4   r   )r0   r6   Úsayr2   r3   Úmemr    r    r!   r   X   s   
r   c              	   c   sN   � t | ƒ| }t|ƒD ]}z| | V  W n ty   Y  dS w ||7 }qdS )zÎGiven 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,   r
   Ú
IndexError)ÚxÚnÚkÚjÚ_r    r    r!   r   k   s   €ÿ
ûr   é   c                 C   s"   t | ƒ}|| kr
|S dj| |d�S )z¨Convert 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)Úintr   )ÚfrB   Úir    r    r!   Úhfloat|   s   rF   c                    s   t ‡ fdd„tD ƒƒS )z4Convert bytes to human-readable form (e.g., KB, MB).c                 3   s8   � | ]\}}ˆ |krd   t|rˆ | nˆ ƒ|¡V  qdS )z{0}{1}N)r   rF   )Ú.0ÚdivÚunit©Úsr    r!   Ú	<genexpr>‰   s   € ÿþzhumanbytes.<locals>.<genexpr>)ÚnextÚUNITSrJ   r    rJ   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ÚrssrA   r    r    r!   r   �   s   ÿr   c                   C   s    t du rtdurtt ¡ ƒa t S )z…Return 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 )N)Úmemory_infoÚAttributeErrorÚget_memory_info)Úprocessr    r    r!   rO   ¢   s
   
ÿrO   ú=é1   c           
      C   sÀ   ddl }| du rtƒ n| } tt| d�}dd„ | ¡ D ƒ}|| }tt ¡ ƒD ]5\}}| |¡}	|	s2q&|d 	|	¡ƒ ||ƒ 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   Nr5   c                 S   s   i | ]}|j |“qS r    )Úident)rG   Útr    r    r!   Ú
<dictcomp>¶   s    zcry.<locals>.<dictcomp>z{0.name}zLOCAL VARIABLES)ÚstreamÚ
)Ú	threadingr   r   r7   Ú	enumerater	   ÚsysÚ_current_framesÚgetr   Ú	tracebackÚprint_stackr   Úf_localsÚgetvalue)
ÚoutÚsepchrÚseplenr_   ÚPÚtmapÚsepÚtidr   Úthreadr    r    r!   r   ©   s$   

r   )r+   )r+   N)r   )r@   )NrX   rY   )&Ú__doc__Ú
__future__r   r   r   rR   ra   rd   Ú
contextlibr   Ú	functoolsr   r   Úcelery.fiver   r	   r
   Úcelery.platformsr   Úpsutilr   ÚImportErrorÚ__all__rN   rQ   r(   r"   r   r   r4   r   r   rF   r   r   r   rO   r   r    r    r    r!   Ú<module>   s@   ÿ	






