3 ^@sxddlZddlZddlmZddlmZddl m Z ddl m Z mZyddlmZWn ek rtddlmZYnXddd d d d d ddddddddddddddgZejZejZejZejZejZy ejZWne k rdZYnXej!Z![da"da#ddZ$ddZ%eZ&ddZGdd d Z'e'Z(Gd!d d Z)Gd"ddZ*Gd#dde*Z+Gd$ddZ,Gd%ddZ-Gd&dde.Z/ej0Z1e1d?d(d)Z2ea3iZ4iZ5e Z6Gd*ddZ7Gd+dde7Z8Gd,d-d-e7Z9Gd.d/d/e7Z:d0d Z;e;Zd2d3Z?d4d Z@dd5lmAZAe9aBd6d7ZCd8d9ZDd:d ZEydd;lmFZGWn"ek rjddZIdS)@N) monotonic) format_exc)WeakSet)islicecount)deque get_ident active_count Conditioncurrent_thread enumerate main_thread TIMEOUT_MAXEventLockRLock SemaphoreBoundedSemaphoreThreadBarrierBrokenBarrierErrorTimer ThreadError setprofilesettracelocal stack_sizecCs|adS)N) _profile_hook)funcr!/usr/lib64/python3.6/threading.pyr2scCs|adS)N) _trace_hook)rrrr r<scOstdkrt||St||S)N)_CRLock_PyRLock)argskwargsrrr rJs  c@sReZdZddZddZdddZeZd d Zd d Zd dZ ddZ ddZ dS)_RLockcCst|_d|_d|_dS)Nr)_allocate_lock_block_owner_count)selfrrr __init__asz_RLock.__init__cCs^|j}yt|j}Wntk r(YnXd|jjr:dnd|jj|jj||j t t |fS)Nz)<%s %s.%s object owner=%r count=%d at %s>lockedZunlocked) r)_activenameKeyErrorr(r- __class__ __module__ __qualname__r*hexid)r+ownerrrr __repr__fsz_RLock.__repr__TcCsDt}|j|kr"|jd7_dS|jj||}|r@||_d|_|S)Nr8)rr)r*r(acquire)r+blockingtimeoutmercrrr r9us z_RLock.acquirecCs<|jtkrtd|jd|_}|s8d|_|jjdS)Nzcannot release un-acquired lockr8)r)r RuntimeErrorr*r(release)r+rrrr r?s  z_RLock.releasecCs |jdS)N)r?)r+tvtbrrr __exit__sz_RLock.__exit__cCs|jj|\|_|_dS)N)r(r9r*r))r+staterrr _acquire_restores z_RLock._acquire_restorecCs<|jdkrtd|j}d|_|j}d|_|jj||fS)Nrzcannot release un-acquired lock)r*r>r)r(r?)r+rr6rrr _release_saves  z_RLock._release_savecCs |jtkS)N)r)r)r+rrr _is_ownedsz_RLock._is_ownedN)TrH) __name__r2r3r,r7r9 __enter__r?rCrErFrGrrrr r&Ws  $ r&c@speZdZdddZddZddZdd Zd d Zd d ZddZ dddZ dddZ dddZ ddZ e ZdS)r NcCs|dkrt}||_|j|_|j|_y |j|_Wntk rDYnXy |j|_Wntk rfYnXy |j|_Wntk rYnXt|_ dS)N) r_lockr9r?rFAttributeErrorrErG_deque_waiters)r+lockrrr r,s$   zCondition.__init__cCs |jjS)N)rKrJ)r+rrr rJszCondition.__enter__cGs |jj|S)N)rKrC)r+r$rrr rCszCondition.__exit__cCsd|jt|jfS)Nz)rKlenrN)r+rrr r7szCondition.__repr__cCs|jjdS)N)rKr?)r+rrr rFszCondition._release_savecCs|jjdS)N)rKr9)r+xrrr rEszCondition._acquire_restorecCs"|jjdr|jjdSdSdS)NrFT)rKr9r?)r+rrr rGs  zCondition._is_ownedcCs|jstdt}|j|jj||j}d}z:|dkrN|jd}n |dkrd|jd|}n |jd}|S|j||sy|jj|Wnt k rYnXXdS)Nzcannot wait on un-acquired lockFTr) rGr>r'r9rNappendrFrEremove ValueError)r+r;waiterZ saved_stateZgotitrrr waits*   zCondition.waitcCs\d}|}|}xH|sV|dk rD|dkr0t|}n|t}|dkrDP|j||}qW|S)Nr)_timerV)r+Z predicater;endtimeZwaittimeresultrrr wait_for7s    zCondition.wait_forr8c Csj|jstd|j}tt||}|s,dSx8|D]0}|jy|j|Wq2tk r`Yq2Xq2WdS)Nz!cannot notify on un-acquired lock)rGr>rNrM_islicer?rSrT)r+nZ all_waitersZwaiters_to_notifyrUrrr notifyNs  zCondition.notifycCs|jt|jdS)N)r]rPrN)r+rrr notify_alleszCondition.notify_all)N)N)N)r8)rIr2r3r,rJrCr7rFrErGrVrZr]r^Z notifyAllrrrr r s  0   c@s4eZdZd ddZd ddZeZdd Zd d ZdS)rr8cCs&|dkrtdtt|_||_dS)Nrz$semaphore initial value must be >= 0)rTr r_cond_value)r+valuerrr r,}s zSemaphore.__init__TNc Cs| r|dk rtdd}d}|jjxb|jdkrv|s8P|dk rh|dkrTt|}n|t}|dkrhP|jj|q(W|jd8_d}WdQRX|S)Nz.can't specify timeout for non-blocking acquireFrr8T)rTr_r`rWrV)r+r:r;r=rXrrr r9s$   zSemaphore.acquirec Cs.|j|jd7_|jjWdQRXdS)Nr8)r_r`r])r+rrr r?szSemaphore.releasecCs |jdS)N)r?)r+r@rArBrrr rCszSemaphore.__exit__)r8)TN)rIr2r3r,r9rJr?rCrrrr rqs   - c@seZdZdddZddZdS)rr8cCstj||||_dS)N)rr,_initial_value)r+rarrr r,s zBoundedSemaphore.__init__c CsB|j2|j|jkrtd|jd7_|jjWdQRXdS)Nz!Semaphore released too many timesr8)r_r`rbrTr])r+rrr r?s  zBoundedSemaphore.releaseN)r8)rIr2r3r,r?rrrr rs c@sBeZdZddZddZddZeZddZd d Zdd d Z d S)rcCstt|_d|_dS)NF)r rr__flag)r+rrr r,s zEvent.__init__cCs|jjtdS)N)r_r,r)r+rrr _reset_internal_locksszEvent._reset_internal_lockscCs|jS)N)rc)r+rrr is_setsz Event.is_setc Cs&|jd|_|jjWdQRXdS)NT)r_rcr^)r+rrr setsz Event.setc Cs|j d|_WdQRXdS)NF)r_rc)r+rrr clear sz Event.clearNc Cs,|j|j}|s|jj|}|SQRXdS)N)r_rcrV)r+r;Zsignaledrrr rVs  z Event.wait)N) rIr2r3r,rdreZisSetrfrgrVrrrr rs   c@s|eZdZdddZdddZddZdd Zd d Zd d ZddZ ddZ ddZ e ddZ e ddZe ddZdS)rNcCs.tt|_||_||_||_d|_d|_dS)Nr)r rr__action_timeout_parties_stater*)r+partiesactionr;rrr r,?s zBarrier.__init__cCs|dkr|j}|jb|j|j}|jd7_z&|d|jkrL|jn |j||S|jd8_|jXWdQRXdS)Nr8)rir__enterr*rj_release_wait_exit)r+r;indexrrr rVOs   z Barrier.waitcCs,x|jdkr|jjqW|jdkr(tdS)Nr8rrH)rHr8)rkr_rVr)r+rrr rnms   zBarrier._enterc Cs>y"|jr|jd|_|jjWn|jYnXdS)Nr8)rhrkr_r^_break)r+rrr roxszBarrier._releasecs4jjfdd|s"jtjdkr0tdS)Ncs jdkS)Nr)rkr)r+rr szBarrier._wait..r)r_rZrsrrk)r+r;r)r+r rps  z Barrier._waitcCs(|jdkr$|jdkr$d|_|jjdS)Nrr8rH)rHr8)r*rkr_r^)r+rrr rqs  z Barrier._exitc CsT|jD|jdkr6|jdkr$d|_q<|jdkr||_n tj|_d|_ d|_ t |_ d|_ d|_tj|_tj|dS)NFT)_targetstrr_name_args_kwargs _daemonicr r_ident _tstate_lockr_started _is_stopped _initialized_sysstderr_stderr _danglingadd)r+grouptargetr/r$r%rrrr r,s  zThread.__init__cCs(|jj|r|jn d|_d|_dS)NT)rrd_set_tstate_lockrr)r+is_aliverrr rd"s   zThread._reset_internal_lockscCs^d}|jjrd}|j|jr$d}|jr2|d7}|jdk rJ|d|j7}d|jj|j|fS)NinitialZstartedZstoppedz daemonz %sz <%s(%s, %s)>) rrerrrrr1rIr)r+Zstatusrrr r7.s  zThread.__repr__cCs|jstd|jjr tdt|t|<WdQRXyt|jfWn,tk rtt t|=WdQRXYnX|jj dS)Nzthread.__init__() not calledz threads can only be started once) rr>rre_active_limbo_lock_limbo_start_new_thread _bootstrap ExceptionrV)r+rrr start<s  z Thread.startc Cs.z|jr|j|j|jWd|`|`|`XdS)N)rrr)r+rrr runUs z Thread.runc Cs2y |jWn |jr&tdkr&dSYnXdS)N)_bootstrap_innerrr)r+rrr rfs  zThread._bootstrapcCs t|_dS)N)rr)r+rrr _set_identzszThread._set_identcCst|_|jjdS)N) _set_sentinelrr9)r+rrr r}szThread._set_tstate_lockc2CszT|j|j|jjt|t|j<t|=WdQRXtrLt j tt rZt j t zy |j Wntk r|Ynt rt jdk rtd|jtft jdn|jdk rH|j\}}}zxtd|jd|jdtd|jdx6|r$td|jjj|j|jjjf|jd|j}qWtd||f|jdWd~~~XYnXWdXWdt y tt=Wn YnXWdQRXXdS)NzException in thread %s: %s)filezException in thread z2 (most likely raised during interpreter shutdown):z"Traceback (most recent call last):z File "%s", line %s, in %sz%s: %s)rrrrfrr.rrr!rrrrr SystemExitrprintr/ _format_excr _exc_infotb_framef_code co_filename tb_linenoco_nametb_nextr)r+exc_type exc_valueZexc_tbrrr rsT         zThread._bootstrap_innercCs|j}|dk rd|_d|_dS)NT)rr)r+rOrrr _stopsz Thread._stopcCsByttt=WdQRXWn tk r<dtjkr8YnXdS)NZdummy_threading)rr.rr0rmodules)r+rrr _deletes  zThread._deletecCsZ|jstd|jjs td|tkr2td|dkrD|jn|jt|dddS)NzThread.__init__() not calledz'cannot join thread before it is startedzcannot join current threadr)r;)rr>rrer _wait_for_tstate_lockmax)r+r;rrr joins   z Thread.joinTr8cCs0|j}|dkrn|j||r,|j|jdS)N)rr9r?r)r+blockr;rOrrr r&s  zThread._wait_for_tstate_lockcCs|jS)N)r)r+rrr r/4s z Thread.namecCst||_dS)N)rr)r+r/rrr r/?scCs|jS)N)r)r+rrr identDs z Thread.identcCs(|js|jj rdS|jd|j S)NF)rrrer)r+rrr rPs  zThread.is_alivecCs|jS)N)r)r+rrr r`sz Thread.daemoncCs*|jstd|jjr td||_dS)NzThread.__init__() not calledz)cannot set daemon status of active thread)rr>rrer)r+daemonicrrr rps  cCs|jS)N)r)r+rrr isDaemonxszThread.isDaemoncCs ||_dS)N)r)r+rrrr setDaemon{szThread.setDaemoncCs|jS)N)r/)r+rrr getName~szThread.getNamecCs ||_dS)N)r/)r+r/rrr setNameszThread.setName)NrH)TrH)rIr2r3rrexc_inforr,rdr7rrrrrrrrrrr{r/setterrrZisAliverrrrrrrrr rs6 , A# &    c@s&eZdZdddZddZddZdS) rNcCsFtj|||_||_|dk r"|ng|_|dk r4|ni|_t|_dS)N)rr,intervalfunctionr$r%rfinished)r+rrr$r%rrr r,s  zTimer.__init__cCs|jjdS)N)rrf)r+rrr cancelsz Timer.cancelcCs6|jj|j|jjs(|j|j|j|jjdS)N)rrVrrerr$r%rf)r+rrr rs z Timer.run)NN)rIr2r3r,rrrrrr rs c@seZdZddZdS) _MainThreadc CsHtj|ddd|j|jj|jt|t|j<WdQRXdS)NZ MainThreadF)r/r) rr,rrrfrrr.r)r+rrr r,s  z_MainThread.__init__N)rIr2r3r,rrrr rsrc@s.eZdZddZddZddZd dd ZdS) _DummyThreadc CsDtj|tddd|jj|jt|t|j<WdQRXdS)NzDummy-%dT)r/r) rr,rrrfrrr.r)r+rrr r,s  z_DummyThread.__init__cCsdS)Nr)r+rrr rsz_DummyThread._stopcCsdS)NTr)r+rrr rsz_DummyThread.is_aliveNcCsdS)Nr)r+r;rrr rsz_DummyThread.join)N)rIr2r3r,rrrrrrr rsrc Cs&y ttStk r tSXdS)N)r.rr0rrrrr r s c Cs tttttSQRXdS)N)rrPr.rrrrr r scCsttjttjS)N)listr.valuesrrrrr _enumeratesrc Cs(tttjttjSQRXdS)N)rrr.rrrrrr r s)rcCs8tj}|jtjt}x|r2|jt}qWdS)N) _main_threadrr?r_pickSomeNonDaemonThreadr)Ztlockr@rrr _shutdownsrcCs(x"tD]}|j r|jr|SqWdS)N)r rr)r@rrr rs rcCstS)N)rrrrr r s)_local)rc Cstai}t}|at|tt}|jtxF|D]>}||kr`|jdt }||_ |||<q4|jd|j q4Wt j tj tj|WdQRXdS)NTF)r'rr rrfrupdaterrdrrrrrgr.)Z new_activeZcurrentZthreadsZthreadrrrr _after_fork(s&        r)r|)Jsysr_threadZtimerrW tracebackrr _weakrefsetr itertoolsrr[rr* _collectionsrrM ImportError collections__all__start_new_threadr allocate_lockr'rrerrorrrr"rLrrr!rrrr&r#r rrrrr>r__next__r}rrr.rrrrrrr Z currentThreadr Z activeCountrr rrrrr rrZ_threading_localrrrrr s           q'P&O '