U
    ‰dÖ  ã                   @   sj   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ G dd„ dejƒZ	e j
dd„ ƒZd
dd	„ZdS )é    N)Ú	viewitemsc                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚWatcherThreadc                 C   s6   t j | ¡ || _d| _t  ¡ | _d| _t  ¡ | _	d S )NFT)
Ú	threadingÚThreadÚ__init__Útimeout_secsÚ	completedÚ	ConditionÚ	conditionÚdaemonÚcurrent_threadÚcaller_thread)Úselfr   © r   ú?/tmp/pip-unpacked-wheel-ua33x9lu/caffe2/python/timeout_guard.pyr      s    
zWatcherThread.__init__c                    sj  t   ¡ }ˆj ¡  t   ¡ | ˆjk rFˆjsFˆj ˆjt   ¡ |  ¡ qˆj ¡  ˆjsft d¡‰ ˆ  	d 
ˆjt ¡ ¡¡ ‡ ‡fdd„}tj|dd}d|_| ¡  td 
ˆj¡ƒ td	ƒ d
d l}d
d l}g }t| ¡ ƒD ]X\}}| d| ¡ | |¡D ]6\}	}
}}| d|	|
|f ¡ |rü| d| ¡  ¡ qüqÜtd |¡ƒ ˆ  d |¡¡ t t ¡ tj¡ d S )NZtimeout_guardz0Call did not finish in time. Timeout:{}s PID: {}c            	         sð   t  d¡ ˆ  d¡ td ˆj¡ƒ tdƒ ˆ  d¡ dd l} dd l}g }t|  	¡ ƒD ]d\}}|ˆjj
krV| d| ¡ | |¡D ]6\}}}}| d|||f ¡ |r‚| d| ¡  ¡ q‚qVtd	 |¡ƒ ˆ  d	 |¡¡ ˆ  d
¡ t ¡  d S )Ng      $@z"Prepared output, dumping threads. úCaller thread was: {}z-----After force------r   ú
# ThreadID: %súFile: "%s", line %d, in %sú  %sÚ
z2Process did not terminate cleanly in 10 s, forcing)ÚtimeÚsleepÚinfoÚprintÚformatr   ÚsysÚ	tracebackr   Ú_current_framesÚidentÚappendÚextract_stackÚstripÚjoinÚerrorÚosÚabort)	r   r   ÚcodeÚthreadIdÚstackÚfilenameÚlinenoÚnameÚline©Úlogr   r   r   Ú	forcequit5   s&    



z$WatcherThread.run.<locals>.forcequitr   )ÚtargetÚargsTr   z-----Before forcing------r   r   r   r   r   )r   r
   Úacquirer   r   ÚwaitÚreleaseÚloggingÚ	getLoggerr#   r   r$   Úgetpidr   r   r   Ústartr   r   r   r   r   r   r   r    r!   r"   r   ÚkillÚsignalÚSIGINT)r   Ústartedr/   Zforcetr   r   r&   r'   r(   r)   r*   r+   r,   r   r-   r   Úrun'   s:    


þzWatcherThread.runN)Ú__name__Ú
__module__Ú__qualname__r   r=   r   r   r   r   r      s   r   c                 c   s>   t | ƒ}| ¡  d V  d|_|j ¡  |j ¡  |j ¡  d S )NT)r   r8   r   r
   r2   Únotifyr4   ©r   Zwatcherr   r   r   ÚCompleteInTimeOrDiea   s    

rC   éx   c                 C   s   t | ƒ}| ¡  dS )z–
    Call this if you have problem with process getting stuck at shutdown.
    It will kill the process if it does not terminate in timeout_secs.
    N)r   r8   rB   r   r   r   ÚEuthanizeIfNecessaryl   s    rE   )rD   )Ú
contextlibr   r$   r   r:   r5   Zfuture.utilsr   r   r   ÚcontextmanagerrC   rE   r   r   r   r   Ú<module>   s   D

