o
    $iD                     @   sP   d dl Z d dlZd dlZd dlZd dlmZmZ eeZ	G dd dej
ZdS )    N)CallableOptionalc                       sJ   e Zd ZdZdddee f fddZddd	Zd
d Zdd Z	  Z
S )PythonGCThreadzA background thread that triggers Python garbage collection.

    This thread waits for GC events from CoreWorker and triggers `gc.collect()` when
    when requested.N)gc_collect_funcr   c                   s:   t d t jddd d| _t | _|ptj	| _
d S )NzStarting Python GC threadr   T)namedaemonF)loggerdebugsuper__init___should_exit	threadingEvent	_gc_eventgccollect_gc_collect_func)selfr   	__class__ \/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/_private/gc_collect_manager.pyr      s
   

zPythonGCThread.__init__returnc                 C   s   | j   d S )N)r   setr   r   r   r   
trigger_gc   s   zPythonGCThread.trigger_gcc              
   C   s   | j sM| j  | j  | j rd S zt }|  }|dkr,td	|t |  W n t
yG } ztd|  W Y d }~nd }~ww | j rd S d S )Nr   z(gc.collect() freed {} refs in {} secondszError during GC: )r   r   waitcleartime	monotonicr   r   r	   format	Exceptionerror)r   start	num_freeder   r   r   run   s(   

zPythonGCThread.runc                 C   s&   t d d| _| j  |   d S )NzStopping Python GC threadT)r   r	   r   r   r   joinr   r   r   r   stop/   s   

zPythonGCThread.stop)r   N)__name__
__module____qualname____doc__r   r   r   r   r&   r(   __classcell__r   r   r   r   r   
   s    
r   )r   loggingr   r   typingr   r   	getLoggerr)   r   Threadr   r   r   r   r   <module>   s    
