3 \@sddlZddlZddlZddlZddlZddlZddlmZddlmZdddgZ Gdd d e Z e Z e j Z e jZe jZe jZd d ZdS) N)spawn)utilensure_runningregister unregisterc@s<eZdZddZddZddZddZd d Zd d Zd S)SemaphoreTrackercCstj|_d|_d|_dS)N) threadingZLock_lock_fd_pid)selfr9/usr/lib64/python3.6/multiprocessing/semaphore_tracker.py__init__s zSemaphoreTracker.__init__cCs|j|jS)N)rr )r rrrgetfd"szSemaphoreTracker.getfdc Cs|j|jdk rRtj|jtj\}}|s0dStj|jd|_d|_tjdg}y|j t j j Wnt k r~YnXd}tj\}}zlyB|j |tj}|gtj}|d||g7}tj|||}Wntj|YnX||_||_Wdtj|XWdQRXdS)zMake sure that semaphore tracker process is running. This can be run from any process. Usually a child process will use the semaphore created by its parent.NzWsemaphore_tracker: process died unexpectedly, relaunching. Some semaphores might leak.z;from multiprocessing.semaphore_tracker import main;main(%d)z-c)r r oswaitpidWNOHANGcloser warningswarnappendsysstderrfileno ExceptionpiperZget_executablerZ_args_from_interpreter_flagsZspawnv_passfds) r pidZstatusZ fds_to_passcmdrwZexeargsrrrr&s8        zSemaphoreTracker.ensure_runningcCs|jd|dS)z2Register name of semaphore with semaphore tracker.ZREGISTERN)_send)r namerrrrQszSemaphoreTracker.registercCs|jd|dS)z4Unregister name of semaphore with semaphore tracker.Z UNREGISTERN)r#)r r$rrrrUszSemaphoreTracker.unregistercCs@|jdj||jd}t|dkr.tdtj|j|}dS)Nz{0}:{1} asciiiz name too long)rformatencodelen ValueErrorrwriter )r rr$msgnbytesrrrr#Ys  zSemaphoreTracker._sendN) __name__ __module__ __qualname__rrrrrr#rrrrrs +rc-Cstjtjtjtjtjtjx6tjtjfD]&}y |jWq.tk rRYq.Xq.Wt }zt |d}x|D]}yJ|j j d\}}|dkr|j |n |dkr|j|n td|Wqrtk rytjtjWn YnXYqrXqrWWdQRXWd|rs   I