B tԼ;@sddlZddlZddlZddlmZddlm Z ddl m Z ddl mZmZyddlmZWn ek r|ddlmZYnXddd d d d d ddddddddddddddgZejZejZejZejZejZy ej Z!Wne"k rdZ!YnXej#Z#[da$da%ddZ&ddZ'eZ(ddZ Gdd d Z)e)Z*Gd!d d Z+Gd"ddZ,Gd#dde,Z-Gd$ddZ.Gd%ddZ/Gd&dde0Z1ej2Z3e3d?d(d)Z4ea5iZ6iZ7e Z8ea9e:a;Gd*ddZGd.d/d/e<Z?d0d Z@e@ZAd1dZBeBZCd2d3ZDd4d ZEdd5lmFZFe>aGd6d7ZHd8d ZIydd9lmJZKWn"ek rvdd:lLmKZKYnXd;d<ZMeNed=rejOeMd>dS)@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,/opt/alt/python37/lib/python3.7/threading.pyr3scCs|adS)N) _trace_hook)rrrr r=scOstdkrt||St||S)N)_CRLock_PyRLock)argskwargsrrr rKs  c@sReZdZddZddZdddZeZd d Zd d Zd dZ ddZ ddZ dS)_RLockcCst|_d|_d|_dS)Nr)_allocate_lock_block_owner_count)selfrrr __init__bsz_RLock.__init__c Cs^|j}yt|j}Wntk r(YnXd|jr: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__gsz_RLock.__repr__TcCsDt}|j|kr"|jd7_dS|j||}|r@||_d|_|S)N)rr)r*r(acquire)r+blockingtimeoutmercrrr r:vs z_RLock.acquirecCs<|jtkrtd|jd|_}|s8d|_|jdS)Nzcannot release un-acquired lockr9)r)r RuntimeErrorr*r(release)r+rrrr r@s  z_RLock.releasecCs |dS)N)r@)r+tvtbrrr __exit__sz_RLock.__exit__cCs|j|\|_|_dS)N)r(r:r*r))r+staterrr _acquire_restores z_RLock._acquire_restorecCs<|jdkrtd|j}d|_|j}d|_|j||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)Tr8) __name__r2r3r,r7r: __enter__r@rDrFrGrHrrrr r&Xs  $ 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_lockr:r@rGAttributeErrorrFrH_deque_waiters)r+lockrrr r,s$   zCondition.__init__cCs |jS)N)rKrJ)r+rrr rJszCondition.__enter__cGs |jj|S)N)rKrD)r+r$rrr rDszCondition.__exit__cCsd|jt|jfS)Nz)rKlenrN)r+rrr r7szCondition.__repr__cCs|jdS)N)rKr@)r+rrr rGszCondition._release_savecCs|jdS)N)rKr:)r+xrrr rFszCondition._acquire_restorecCs"|jdr|jdSdSdS)NrFT)rKr:r@)r+rrr rHs  zCondition._is_ownedcCs|stdt}||j||}d}z:|dkrN|d}n |dkrd|d|}n |d}|S|||sy|j|Wnt k rYnXXdS)Nzcannot wait on un-acquired lockFTr) rHr?r'r:rNappendrGrFremove ValueError)r+r<waiterZ saved_stateZgotitrrr waits*   zCondition.waitcCs\d}|}|}xH|sV|dk rD|dkr0t|}n|t}|dkrDP|||}qW|S)Nr)_timerV)r+Z predicater<endtimeZwaittimeresultrrr wait_for8s    zCondition.wait_forr9c Csj|std|j}tt||}|s,dSx8|D]0}|y||Wq2tk r`Yq2Xq2WdS)Nz!cannot notify on un-acquired lock)rHr?rNrM_islicer@rSrT)r+nZ all_waitersZwaiters_to_notifyrUrrr notifyOs  zCondition.notifycCs|t|jdS)N)r]rPrN)r+rrr notify_allfszCondition.notify_all)N)N)N)r9)rIr2r3r,rJrDr7rGrFrHrVrZr]r^Z notifyAllrrrr r s  0   c@s4eZdZd ddZd ddZeZdd Zd d ZdS)rr9cCs&|dkrtdtt|_||_dS)Nrz$semaphore initial value must be >= 0)rTr r_cond_value)r+valuerrr r,~s zSemaphore.__init__TNc Cs|s|dk rtdd}d}|jjxb|jdkrt|s6P|dk rf|dkrRt|}n|t}|dkrfP|j|q&W|jd8_d}WdQRX|S)Nz.can't specify timeout for non-blocking acquireFrr9T)rTr_r`rWrV)r+r;r<r>rXrrr r:s$    zSemaphore.acquirec Cs.|j|jd7_|jWdQRXdS)Nr9)r_r`r])r+rrr r@szSemaphore.releasecCs |dS)N)r@)r+rArBrCrrr rDszSemaphore.__exit__)r9)TN)rIr2r3r,r:rJr@rDrrrr rrs   - c@seZdZdddZddZdS)rr9cCst||||_dS)N)rr,_initial_value)r+rarrr r,s zBoundedSemaphore.__init__c CsB|j2|j|jkrtd|jd7_|jWdQRXdS)Nz!Semaphore released too many timesr9)r_r`rbrTr])r+rrr r@s  zBoundedSemaphore.releaseN)r9)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|jtdS)N)r_r,r)r+rrr _reset_internal_locksszEvent._reset_internal_lockscCs|jS)N)rc)r+rrr is_setsz Event.is_setc Cs&|jd|_|jWdQRXdS)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|j|}|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__c Cs|dkr|j}|jb||j}|jd7_z&|d|jkrL|n |||S|jd8_|XWdQRXdS)Nr9)rir__enterr*rj_release_wait_exit)r+r<indexrrr rVPs   z Barrier.waitcCs,x|jdkr|jqW|jdkr(tdS)N)r8r9r)rkr_rVr)r+rrr rnns   zBarrier._entercCs>y"|jr|d|_|jWn|YnXdS)Nr9)rhrkr_r^_break)r+rrr royszBarrier._releasecs4jfdd|s"tjdkr0tdS)Ncs jdkS)Nr)rkr)r+rr zBarrier._wait..r)r_rZrsrrk)r+r<r)r+r rps  z Barrier._waitcCs(|jdkr$|jdkr$d|_|jdS)Nr)r8r9)r*rkr_r^)r+rrr rqs  z Barrier._exitc CsT|jD|jdkr6|jdkr$d|_q<|jdkr||_n tj|_d|_ d|_ t |_ d|_ d|_tj|_t|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(|j|r|n d|_d|_dS)NT)rrd_set_tstate_lockrr)r+is_aliverrr rd(s   zThread._reset_internal_lockscCs^d}|jrd}||jr$d}|jr2|d7}|jdk rJ|d|j7}d|jj|j|fS)NinitialZstartedZstoppedz daemonz %sz <%s(%s, %s)>) rrerrrrr1rIr)r+statusrrr r74s  zThread.__repr__c Cs|jstd|jr tdt|t|<WdQRXyt|jdWn,tk rtt t|=WdQRXYnX|j dS)Nzthread.__init__() not calledz threads can only be started oncer) rr?rre_active_limbo_lock_limbo_start_new_thread _bootstrap ExceptionrV)r+rrr startBs  z Thread.startcCs.z|jr|j|j|jWd|`|`|`XdS)N)rrr)r+rrr run[s z Thread.runcCs2y |Wn |jr&tdkr&dSYnXdS)N)_bootstrap_innerrr)r+rrr rls  zThread._bootstrapcCs t|_dS)N)rr)r+rrr _set_identszThread._set_identc Cs8t|_|j|js4tt|jWdQRXdS)N) _set_sentinelrr:r_shutdown_locks_lock_shutdown_locksr)r+rrr rs  zThread._set_tstate_lockcCsz`|||jt|t|j<t|=WdQRXtrLt tt rZt t zy | Wntk r~Ynt rt jdk rtd|jtft jdn|jdk rT|\}}}ztd|jd|jdtd|jdx6|r&td|jjj|j|jjjf|jd|j}qWtd||f|jd|jWd~~~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_nextflushr)r+exc_type exc_valueZexc_tbrrr rsV         zThread._bootstrap_innerc Cs>|j}|dk rd|_d|_|js:tt|WdQRXdS)NT)rrrrrdiscard)r+rOrrr _stopsz Thread._stopc Csttt=WdQRXdS)N)rr.r)r+rrr _deleteszThread._deletecCsZ|jstd|js td|tkr2td|dkrD|n|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|||r,||dS)N)rr:r@r)r+blockr<rOrrr rs  zThread._wait_for_tstate_lockcCs|jS)N)r)r+rrr r/(s z Thread.namecCst||_dS)N)rr)r+r/rrr r/3scCs|jS)N)r)r+rrr ident8s z Thread.identcCs&|js|jsdS|d|j S)NF)rrrer)r+rrr rDs  zThread.is_alivecCs ddl}|jdtdd|S)Nrz/isAlive() is deprecated, use is_alive() instead) stacklevel)warningswarnPendingDeprecationWarningr)r+rrrr isAliveRs zThread.isAlivecCs|jS)N)r)r+rrr r\s z Thread.daemoncCs*|jstd|jr td||_dS)NzThread.__init__() not calledz)cannot set daemon status of active thread)rr?rrer)r+daemonicrrr rks  cCs|jS)N)r)r+rrr isDaemonsszThread.isDaemoncCs ||_dS)N)r)r+rrrr setDaemonvszThread.setDaemoncCs|jS)N)r/)r+rrr getNameyszThread.getNamecCs ||_dS)N)r/)r+r/rrr setName|szThread.setName)NNNrN)N)Tr8)rIr2r3rrexc_inforr,rdr7rrrrrrrrrrr{r/setterrrrrrrrrrrrr rs6 ,  B &    c@s&eZdZdddZddZddZdS) rNcCsFt|||_||_|dk r"|ng|_|dk r4|ni|_t|_dS)N)rr,intervalfunctionr$r%rfinished)r+rrr$r%rrr r,s  zTimer.__init__cCs|jdS)N)rrf)r+rrr cancelsz Timer.cancelcCs6|j|j|js(|j|j|j|jdS)N)rrVrrerr$r%rf)r+rrr rs z Timer.run)NN)rIr2r3r,rrrrrr rs c@seZdZddZdS) _MainThreadc CsHtj|ddd||j|t|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|j|t|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 rsrcCs&y ttStk r tSXdS)N)r.rr0rrrrr r s c Cs tttttSQRXdS)N)rrPr.rrrrr r scCsttttS)N)listr.valuesrrrrr _enumeratesrc Cs(tttttSQRXdS)N)rrr.rrrrrr r s)rc Csntjr dStj}|txHttt}tWdQRX|sHPx|D]}| |qNWq"WdS)N) _main_threadrrr@rrrrrgr:)ZtlockZlocksrOrrr _shutdowns  rcCstS)N)rrrrr r "s)_local)rc Cstai}ytt}Wntk r2t}YnX|atata t|tt }| t xF|D]>}||kr| dt}||_|||<qd| d|qdWttt |WdQRXdS)NTF)r'rr.rr0rrrrfrrupdaterrdrrrrg)Z new_activeZcurrentZthreadsZthreadrrrr _after_fork3s0         rregister_at_fork)Zafter_in_child)r|)Pos_ossysr_threadZtimerrW tracebackrrZ _weakrefsetr itertoolsrr[rr* _collectionsrrM ImportError collections__all__start_new_threadr allocate_lockr'rrerrorrrr"rLrrr!rrrr&r#r rrrrr?r__next__r}rrr.rrrrfrrrrrr Z currentThreadr Z activeCountrr rrrr rrZ_threading_localrhasattrrrrrr s           q'P&O    ( 5