B
    H/\}.                 @   s   d dl mZmZm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 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 dgZeedZeedZeddZG dd de Z!G dd de"Z#eeG dd de"Z$dS )    )print_functionabsolute_importdivisionN)ref)
getcurrent)config)get_original)notify)EventLoopBlocked)MemoryUsageThresholdExceeded)MemoryUsageUnderThreshold)IPeriodicMonitorThread)implementer)GreenletTracer)thread_mod_name)perf_counterPeriodicMonitoringThread	get_identstart_new_threadZtimeZsleepc               @   s   e Zd ZdZdS )MonitorWarningzThe type of warnings we emit.N)__name__
__module____qualname____doc__ r   r   .lib/python3.7/site-packages/gevent/_monitor.pyr   $   s   r   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	_MonitorEntry)functionperiodlast_run_timec             C   s   || _ || _d| _d S )Nr   )r   r   r   )selfr   r   r   r   r   __init__,   s    z_MonitorEntry.__init__c             C   s   | j |j ko| j|jkS )N)r   r   )r    otherr   r   r   __eq__1   s    z_MonitorEntry.__eq__c             C   s   t | j| j| jfS )N)reprr   r   r   )r    r   r   r   __repr__4   s    z_MonitorEntry.__repr__N)r   r   r   	__slots__r!   r#   r%   r   r   r   r   r   (   s   r   c               @   s   e Zd ZdZdZdZdZdZdZdZ	dd Z
dd	 Zed
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' ZdS )(r   g       @g{Gzt?   Nr   c             C   s`   t || j| _d| _t |jks$tt | _t	| j
tjg| _tj| _t| d| _t | _d S )NTr   )wref
_on_hub_gc	_hub_wref
should_runget_thread_identZthread_identAssertionErrorr   _greenlet_tracerr   monitor_blockingGEVENT_CONFIGmax_blocking_time_monitoring_functions_calculated_sleep_timer   monitor_thread_identosgetpidpid)r    hubr   r   r   r!   X   s    z!PeriodicMonitoringThread.__init__c             C   s(   t  }|| jkr$|| _t| d| _d S )Nr   )r5   r6   r7   r   r4   )r    r7   r   r   r   _on_forkk   s    
z!PeriodicMonitoringThread._on_forkc             C   s   |   S )N)r*   )r    r   r   r   r8   s   s    zPeriodicMonitoringThread.hubc             C   s>   t j}|| jd jkr8|| jd _tdd | jD | _| jS )Nr   c             s   s   | ]}|j V  qd S )N)r   ).0xr   r   r   	<genexpr>   s    z@PeriodicMonitoringThread.monitoring_functions.<locals>.<genexpr>)r0   r1   r2   r   minr3   )r    Zmbtr   r   r   monitoring_functionsx   s
    z-PeriodicMonitoringThread.monitoring_functionsc                s   t std|d kr0fdd| jD | _nJ|dkrBtdn8t|  fdd| jD | _ | jkrz| j  tdd | jD | _d S )	Nzfunction must be callablec                s   g | ]}|j  kr|qS r   )r   )r:   r;   )r   r   r   
<listcomp>   s    zDPeriodicMonitoringThread.add_monitoring_function.<locals>.<listcomp>r   zPeriod must be positive.c                s   g | ]}|j kr|n qS r   )r   )r:   r;   )entryr   r   r   r?      s   c             s   s   | ]}|j V  qd S )N)r   )r:   r;   r   r   r   r<      s    zCPeriodicMonitoringThread.add_monitoring_function.<locals>.<genexpr>)callable
ValueErrorr2   r   appendr=   r3   )r    r   r   r   )r@   r   r   add_monitoring_function   s    


z0PeriodicMonitoringThread.add_monitoring_functionc             C   s"   | j }|dkr| jS t|| jfS )Nr   )r3   inactive_sleep_timemaxmin_sleep_time)r    Z	min_sleepr   r   r   calculate_sleep_time   s    z-PeriodicMonitoringThread.calculate_sleep_timec             C   s   | j s
d S d| _ | j  d S )NF)r+   r.   kill)r    r   r   r   rI      s    zPeriodicMonitoringThread.killc             C   s   |    d S )N)rI   )r    _r   r   r   r)      s    z#PeriodicMonitoringThread._on_hub_gcc       	      C   s   t | t _yx| jr|  }|s&t|  }t| | j}|sH| 	  | jrt
 }x<|D ]4}|j}|j}|j}|rZ|| |krZ||_|| qZW ~qW W nL tk
r   Y n:   trtjsn"| j}|d k	r|j| ft   Y nX d S )N)r(   r   Zgevent_monitoring_threadr+   r>   r-   rH   thread_sleepr8   rI   r   r   r   r   
SystemExitsysstderrZhandle_errorexc_info)	r    Z	functionsZ
sleep_timer8   Zthis_runr@   fr   Zlast_runr   r   r   __call__   s8    


z!PeriodicMonitoringThread.__call__c             C   sp   | j |}|sd S |d }| j ||td| jd}|j}x|D ]}t||d qBW tt|t	j
| ||fS )N   F)Zgreenlet_stacksZcurrent_thread_ident)file)r.   Zdid_block_hubZdid_block_hub_reportdictr4   Zexception_streamprintr	   r
   r0   r1   )r    r8   Z	did_blockZactive_greenletZreportstreamliner   r   r   r/      s    
z)PeriodicMonitoringThread.monitor_blockingc             C   s   | j   d S )N)r.    ignore_current_greenlet_blocking)r    r   r   r   rX      s    z9PeriodicMonitoringThread.ignore_current_greenlet_blockingc             C   s   | j   d S )N)r.   !monitor_current_greenlet_blocking)r    r   r   r   rY      s    z:PeriodicMonitoringThread.monitor_current_greenlet_blockingc                sl   y@ddl m}m} y|     W n |k
r<   d  Y nX W n tk
rX   d  Y nX  fdd| _ S )Nr   )ProcessAccessDeniedc                  s    S )Nr   r   )procr   r   <lambda>  s    z7PeriodicMonitoringThread._get_process.<locals>.<lambda>)ZpsutilrZ   r[   memory_full_infoImportError_get_process)r    rZ   r[   r   )r\   r   r`      s    
z%PeriodicMonitoringThread._get_processc             C   s   |   d k	S )N)r`   )r    r   r   r   can_monitor_memory_usage  s    z1PeriodicMonitoringThread.can_monitor_memory_usagec             C   s<   |   s dd l}|dt d S | | jttj| j	 d S )Nr   z/Unable to monitor memory usage. Install psutil.)
ra   warningswarnr   rD   monitor_memory_usagerF   r0   Zmemory_monitor_periodmin_memory_monitor_period)r    rb   r   r   r   install_monitor_memory_usage  s    z5PeriodicMonitoringThread.install_monitor_memory_usagec             C   s   t j}|sdS |   }t|ddp*|j}d }||kr^|| jkrVt|||}t| || _n$| jr|t	|||| j}t| d| _|S )NZussr   )
r0   Zmax_memory_usager`   r^   getattrZrss_memory_exceededr   r	   r   )r    Z_hubZmax_allowedZrusageZ	mem_usageZeventr   r   r   rd   !  s$    

z-PeriodicMonitoringThread.monitor_memory_usagec             C   s*   d| j jtt| t| jt |  f S )Nz*<%s at %s in thread %s greenlet %r for %r>)	__class__r   hexidr4   r   r*   )r    r   r   r   r%   ?  s    
z!PeriodicMonitoringThread.__repr__)r   r   r   rE   rG   re   r2   r3   ri   r.   r!   r9   propertyr8   r>   rD   rH   rI   r)   rQ   r/   rX   rY   r`   ra   rf   rd   r%   r   r   r   r   r   8   s0   
		.)%Z
__future__r   r   r   r5   rM   weakrefr   r(   Zgreenletr   Zgeventr   r0   Zgevent.monkeyr   Zgevent.eventsr	   r
   r   r   r   r   Zgevent._tracerr   Zgevent._compatr   r   __all__r,   r   rK   RuntimeWarningr   objectr   r   r   r   r   r   <module>   s.   


