[@sddlZddlZddlZddlZddlZddlZddlmZddlmZdddgZ Gdd d e Z e Z e j Z e jZe jZe jZd d ZdS) N)spawn)utilensure_runningregister unregisterc@sXeZdZddZddZddZddZd d Zd d Zd S)SemaphoreTrackercCstj|_d|_dS)N) threadingZLock_lock_fd)selfr 7/usr/lib/python3.4/multiprocessing/semaphore_tracker.py__init__szSemaphoreTracker.__init__cCs|j|jS)N)rr )r r r rgetfd!s zSemaphoreTracker.getfdcCs|j|jdk rdSg}y|jtjjWntk rQYnXd}tj\}}zyW|j|t j }|gt j }|d||g7}t j |||Wntj|Yn X||_Wdtj|XWdQXdS)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.Nz;from multiprocessing.semaphore_tracker import main;main(%d)z-c)r r appendsysstderrfileno ExceptionospiperZget_executablerZ_args_from_interpreter_flagsZspawnv_passfdsclose)r Z fds_to_passcmdrwZexeargsr r rr%s*      zSemaphoreTracker.ensure_runningcCs|jd|dS)z2Register name of semaphore with semaphore tracker.ZREGISTERN)_send)r namer r rrCszSemaphoreTracker.registercCs|jd|dS)z4Unregister name of semaphore with semaphore tracker.Z UNREGISTERN)r)r rr r rrGszSemaphoreTracker.unregistercCsw|jdj||jd}t|dkrFtdntj|j|}|t|ksstdS)Nz{0}:{1} asciiiz name too long) rformatencodelen ValueErrorrwriter AssertionError)r rrmsgnbytesr r rrKs  zSemaphoreTracker._sendN) __name__ __module__ __qualname__rrrrrrr r r rrs      rc,Cstjtjtjtjtjtjx=tjtjfD])}y|jWq?tk rgYq?Xq?Wt }zt |d}x|D]}yg|j j d\}}|dkr|j |n,|dkr|j|ntd|Wqtk r4ytjtjWnYnXYqXqWWdQXWd|r|ytjdt|Wq|tk rxYq|Xnxu|D]m}zc|jd}ytj|Wn<tk r}ztjd ||fWYdd}~XnXWdXqWXdS) zRun semaphore tracker.rb:sREGISTERs UNREGISTERzunrecognized command %rNzRsemaphore_tracker: There appear to be %d leaked semaphores to clean up at shutdownrzsemaphore_tracker: %r: %s)signalSIGINTSIG_IGNSIGTERMrstdinstdoutrrsetopenstripsplitaddremove RuntimeError excepthookexc_infowarningswarnr"decode_multiprocessingZ sem_unlink)fdfcachelinerrer r rmain]sJ          .rE)rr-rr r<r?rr__all__objectrZ_semaphore_trackerrrrrrEr r r rs      ;