zfc@s=dZddlZyddlZWnek rBeje=nXddlZddlm Z ddl m Z ddlmZmZddlmZddd d d d d ddddddddddgZejZejZejZejZ[ejddedddde Z!de"fdYZ#e$a%e$a&d Z'd!Z(eZ)d"Z*d#e#fd$YZ+d%Z,d&e#fd'YZ-d(Z.d)e#fd*YZ/d+Z0d,e/fd-YZ1d.Z2d/e#fd0YZ3e j4Z5e5d1d2Z6ea7iZ8iZ9de#fd3YZ:d4Z;d5e:fd6YZ<d7e:fd8YZ=d9Z>d:e:fd;YZ?d<Z@e@ZAd=ZBeBZCd>ZDd?ZEdd@lmFZFe=jGZHyddAlmIZJWn!ek rddBlKmJZJnXdCZLdDZMedEkr9eMndS(Fs;Thread module emulating a subset of Java's threading model.iN(tdeque(tcount(ttimetsleep(t format_exct activeCountt active_countt Conditiont currentThreadtcurrent_threadt enumeratetEventtLocktRLockt SemaphoretBoundedSemaphoretThreadtTimert setprofiletsettracetlocalt stack_sizetignoretcategorytmodulet threadingtmessages sys.exc_cleart_VerbosecBseZddZdZRS(cCs"|dkrt}n||_dS(N(tNonet_VERBOSEt_Verbose__verbose(tselftverbose((s!/usr/lib64/python2.7/threading.pyt__init__;s  cGsr|jrn||}t}yt|j}Wntk rJd|}nXd||f}tjj|ndS(Nss%s: %s (Rt _get_identt_activetnametKeyErrort_syststderrtwrite(RtformattargstidentR$((s!/usr/lib64/python2.7/threading.pyt_note@s    N(t__name__t __module__RR!R,(((s!/usr/lib64/python2.7/threading.pyR9s cCs |adS(sSet a profile function for all threads started from the threading module. The func will be passed to sys.setprofile() for each thread, before its run() method is called. N(t _profile_hook(tfunc((s!/usr/lib64/python2.7/threading.pyRZscCs |adS(sSet a trace function for all threads started from the threading module. The func will be passed to sys.settrace() for each thread, before its run() method is called. N(t _trace_hook(R0((s!/usr/lib64/python2.7/threading.pyRdscOs t||S(s2Factory function that returns a new reentrant lock. A reentrant lock must be released by the thread that acquired it. Once a thread has acquired a reentrant lock, the same thread may acquire it again without blocking; the thread must release it once for each time it has acquired it. (t_RLock(R*tkwargs((s!/usr/lib64/python2.7/threading.pyR rs R2cBsbeZdZd dZdZddZeZdZdZ dZ dZ d Z RS( sA reentrant lock must be released by the thread that acquired it. Once a thread has acquired a reentrant lock, the same thread may acquire it again without blocking; the thread must release it once for each time it has acquired it. cCs2tj||t|_d|_d|_dS(Ni(RR!t_allocate_lockt _RLock__blockRt _RLock__ownert _RLock__count(RR ((s!/usr/lib64/python2.7/threading.pyR!s  cCsH|j}yt|j}Wntk r-nXd|jj||jfS(Ns<%s owner=%r count=%d>(R6R#R$R%t __class__R-R7(Rtowner((s!/usr/lib64/python2.7/threading.pyt__repr__s  icCst}|j|kr?|jd|_|jd||dS|jj|}|r||_d|_|jd||n|jd|||S(sAcquire a lock, blocking or non-blocking. When invoked without arguments: if this thread already owns the lock, increment the recursion level by one, and return immediately. Otherwise, if another thread owns the lock, block until the lock is unlocked. Once the lock is unlocked (not owned by any thread), then grab ownership, set the recursion level to one, and return. If more than one thread is blocked waiting until the lock is unlocked, only one at a time will be able to grab ownership of the lock. There is no return value in this case. When invoked with the blocking argument set to true, do the same thing as when called without arguments, and return true. When invoked with the blocking argument set to false, do not block. If a call without an argument would block, return false immediately; otherwise, do the same thing as when called without arguments, and return true. is!%s.acquire(%s): recursive successs%s.acquire(%s): initial successs%s.acquire(%s): failure(R"R6R7R,R5tacquire(Rtblockingtmetrc((s!/usr/lib64/python2.7/threading.pyR;s   cCsx|jtkr!tdn|jd|_}|sdd|_|jj|jd|n|jd|dS(smRelease a lock, decrementing the recursion level. If after the decrement it is zero, reset the lock to unlocked (not owned by any thread), and if any other threads are blocked waiting for the lock to become unlocked, allow exactly one of them to proceed. If after the decrement the recursion level is still nonzero, the lock remains locked and owned by the calling thread. Only call this method when the calling thread owns the lock. A RuntimeError is raised if this method is called when the lock is unlocked. There is no return value. scannot release un-acquired lockis%s.release(): final releases%s.release(): non-final releaseN(R6R"t RuntimeErrorR7RR5treleaseR,(RR((s!/usr/lib64/python2.7/threading.pyR@s  cCs|jdS(N(R@(Rtttvttb((s!/usr/lib64/python2.7/threading.pyt__exit__scCs?|\}}|jj||_||_|jd|dS(Ns%s._acquire_restore()(R5R;R7R6R,(Rt count_ownerRR9((s!/usr/lib64/python2.7/threading.pyt_acquire_restores     cCsK|jd||j}d|_|j}d|_|jj||fS(Ns%s._release_save()i(R,R7R6RR5R@(RRR9((s!/usr/lib64/python2.7/threading.pyt _release_saves     cCs|jtkS(N(R6R"(R((s!/usr/lib64/python2.7/threading.pyt _is_ownedsN( R-R.t__doc__RR!R:R;t __enter__R@RDRFRGRH(((s!/usr/lib64/python2.7/threading.pyR2}s  &    cOs t||S(s}Factory function that returns a new condition variable object. A condition variable allows one or more threads to wait until they are notified by another thread. If the lock argument is given and not None, it must be a Lock or RLock object, and it is used as the underlying lock. Otherwise, a new RLock object is created and used as the underlying lock. (t _Condition(R*R3((s!/usr/lib64/python2.7/threading.pyRs RKcBs}eZdZd d dZdZdZdZdZdZ dZ d e dZ d d Z d ZeZRS( slCondition variables allow one or more threads to wait until they are notified by another thread. cCstj|||dkr(t}n||_|j|_|j|_y|j|_Wntk rlnXy|j |_ Wntk rnXy|j |_ Wntk rnXg|_ dS(N( RR!RR t_Condition__lockR;R@RGtAttributeErrorRFRHt_Condition__waiters(RtlockR ((s!/usr/lib64/python2.7/threading.pyR!s&        cCs |jjS(N(RLRJ(R((s!/usr/lib64/python2.7/threading.pyRJscGs|jj|S(N(RLRD(RR*((s!/usr/lib64/python2.7/threading.pyRD scCsd|jt|jfS(Ns(RLtlenRN(R((s!/usr/lib64/python2.7/threading.pyR:#scCs|jjdS(N(RLR@(R((s!/usr/lib64/python2.7/threading.pyRG&scCs|jjdS(N(RLR;(Rtx((s!/usr/lib64/python2.7/threading.pyRF)scCs+|jjdr#|jjtStSdS(Ni(RLR;R@tFalsetTrue(R((s!/usr/lib64/python2.7/threading.pyRH,s c Cs~|jstdnt}|j|jj||j}z|d krv|j|jd|nt |}d}xt r|jd}|rPnt |t |}|dkrPn|rt |d|d}n|}t |}t |qW|sU|jd||y|jj |Wqhtk rQqhXn|jd||Wd |j|Xd S( sjWait until notified or until a timeout occurs. If the calling thread has not acquired the lock when this method is called, a RuntimeError is raised. This method releases the underlying lock, and then blocks until it is awakened by a notify() or notifyAll() call for the same condition variable in another thread, or until the optional timeout occurs. Once awakened or timed out, it re-acquires the lock and returns. When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof). When the underlying lock is an RLock, it is not released using its release() method, since this may not actually unlock the lock when it was acquired multiple times recursively. Instead, an internal interface of the RLock class is used, which really unlocks it even when it has been recursively acquired several times. Another internal interface is then used to restore the recursion level when the lock is reacquired. scannot wait on un-acquired locks%s.wait(): got itgMb@?iig?s%s.wait(%s): timed outs%s.wait(%s): got itN(RHR?R4R;RNtappendRGRR,t_timeRStmint_sleeptremovet ValueErrorRF( Rttimeoutt balancingtwaitert saved_statetendtimetdelaytgotitt remaining((s!/usr/lib64/python2.7/threading.pytwait5s@           icCs|jstdn|j}|| }|sH|jd|dS|jd|||dkridpldx=|D]5}|jy|j|Wqwtk rqwXqwWdS(sKWake up one or more threads waiting on this condition, if any. If the calling thread has not acquired the lock when this method is called, a RuntimeError is raised. This method wakes up at most n of the threads waiting for the condition variable; it is a no-op if no threads are waiting. s!cannot notify on un-acquired locks%s.notify(): no waitersNs"%s.notify(): notifying %d waiter%sitst(RHR?RNR,R@RXRY(RtnRNtwaitersR\((s!/usr/lib64/python2.7/threading.pytnotifyys      cCs|jt|jdS(sWake up all threads waiting on this condition. If the calling thread has not acquired the lock when this method is called, a RuntimeError is raised. N(RgRPRN(R((s!/usr/lib64/python2.7/threading.pyt notifyAllsN(R-R.RIRR!RJRDR:RGRFRHRSRbRgRht notify_all(((s!/usr/lib64/python2.7/threading.pyRKs      D  cOs t||S(sOA factory function that returns a new semaphore. Semaphores manage a counter representing the number of release() calls minus the number of acquire() calls, plus an initial value. The acquire() method blocks if necessary until it can return without making the counter negative. If not given, value defaults to 1. (t _Semaphore(R*R3((s!/usr/lib64/python2.7/threading.pyRs RjcBsAeZdZdddZddZeZdZdZRS(s"Semaphores manage a counter representing the number of release() calls minus the number of acquire() calls, plus an initial value. The acquire() method blocks if necessary until it can return without making the counter negative. If not given, value defaults to 1. icCsJ|dkrtdntj||tt|_||_dS(Nis$semaphore initial value must be >= 0(RYRR!RR t_Semaphore__condt_Semaphore__value(RtvalueR ((s!/usr/lib64/python2.7/threading.pyR!s  c Cst}|jwxo|jdkrU|s,Pn|jd|||j|jjqW|jd|_|jd||jt}WdQX|S(sAcquire a semaphore, decrementing the internal counter by one. When invoked without arguments: if the internal counter is larger than zero on entry, decrement it by one and return immediately. If it is zero on entry, block, waiting until some other thread has called release() to make it larger than zero. This is done with proper interlocking so that if multiple acquire() calls are blocked, release() will wake exactly one of them up. The implementation may pick one at random, so the order in which blocked threads are awakened should not be relied on. There is no return value in this case. When invoked with blocking set to true, do the same thing as when called without arguments, and return true. When invoked with blocking set to false, do not block. If a call without an argument would block, return false immediately; otherwise, do the same thing as when called without arguments, and return true. is)%s.acquire(%s): blocked waiting, value=%sis%s.acquire: success, value=%sN(RRRkRlR,RbRS(RR<R>((s!/usr/lib64/python2.7/threading.pyR;s     cCsG|j8|jd|_|jd||j|jjWdQXdS(sRelease a semaphore, incrementing the internal counter by one. When the counter is zero on entry and another thread is waiting for it to become larger than zero again, wake up that thread. is%s.release: success, value=%sN(RkRlR,Rg(R((s!/usr/lib64/python2.7/threading.pyR@s    cCs|jdS(N(R@(RRARBRC((s!/usr/lib64/python2.7/threading.pyRDsN( R-R.RIRR!R;RJR@RD(((s!/usr/lib64/python2.7/threading.pyRjs  % cOs t||S(sA factory function that returns a new bounded semaphore. A bounded semaphore checks to make sure its current value doesn't exceed its initial value. If it does, ValueError is raised. In most situations semaphores are used to guard resources with limited capacity. If the semaphore is released too many times it's a sign of a bug. If not given, value defaults to 1. Like regular semaphores, bounded semaphores manage a counter representing the number of release() calls minus the number of acquire() calls, plus an initial value. The acquire() method blocks if necessary until it can return without making the counter negative. If not given, value defaults to 1. (t_BoundedSemaphore(R*R3((s!/usr/lib64/python2.7/threading.pyRsRncBs&eZdZdddZdZRS(sA bounded semaphore checks to make sure its current value doesn't exceed its initial value. If it does, ValueError is raised. In most situations semaphores are used to guard resources with limited capacity. icCs tj|||||_dS(N(RjR!t_initial_value(RRmR ((s!/usr/lib64/python2.7/threading.pyR! scCsQ|jB|j|jkr+tdn|jd7_|jjWdQXdS(s6Release a semaphore, incrementing the internal counter by one. When the counter is zero on entry and another thread is waiting for it to become larger than zero again, wake up that thread. If the number of releases exceeds the number of acquires, raise a ValueError. s!Semaphore released too many timesiN(RkRlRoRYRg(R((s!/usr/lib64/python2.7/threading.pyR@s N(R-R.RIRR!R@(((s!/usr/lib64/python2.7/threading.pyRnscOs t||S(sA factory function that returns a new event. Events manage a flag that can be set to true with the set() method and reset to false with the clear() method. The wait() method blocks until the flag is true. (t_Event(R*R3((s!/usr/lib64/python2.7/threading.pyR "sRpcBsSeZdZddZdZdZeZdZdZ de dZ RS(sA factory function that returns a new event object. An event manages a flag that can be set to true with the set() method and reset to false with the clear() method. The wait() method blocks until the flag is true. cCs/tj||tt|_t|_dS(N(RR!RR t _Event__condRRt _Event__flag(RR ((s!/usr/lib64/python2.7/threading.pyR!5scCs|jjtdS(N(RqR!R (R((s!/usr/lib64/python2.7/threading.pyt_reset_internal_locks:scCs|jS(s5Return true if and only if the internal flag is true.(Rr(R((s!/usr/lib64/python2.7/threading.pytisSet>scCs*|jt|_|jjWdQXdS(sSet the internal flag to true. All threads waiting for the flag to become true are awakened. Threads that call wait() once the flag is true will not block at all. N(RqRSRrRi(R((s!/usr/lib64/python2.7/threading.pytsetDs  cCs|jt|_WdQXdS(sReset the internal flag to false. Subsequently, threads calling wait() will block until set() is called to set the internal flag to true again. N(RqRRRr(R((s!/usr/lib64/python2.7/threading.pytclearOs cCs:|j+|js)|jj||n|jSWdQXdS(sHBlock until the internal flag is true. If the internal flag is true on entry, return immediately. Otherwise, block until another thread calls set() to set the flag to true, or until the optional timeout occurs. When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof). This method returns the internal flag on exit, so it will always return True except if a timeout is given and the operation times out. N(RqRrRb(RRZR[((s!/usr/lib64/python2.7/threading.pyRbYs  N( R-R.RIRR!RsRttis_setRuRvRSRb(((s!/usr/lib64/python2.7/threading.pyRp,s    s Thread-%dcCs |tS(N(t_counter(ttemplate((s!/usr/lib64/python2.7/threading.pyt_newnamepscBs=eZdZeZejZejZ dddddddZ dZ e dZdZdZdZdZdZd Zd Zd Zd Zded Ze dZejdZe dZdZeZe dZejdZdZ dZ!dZ"dZ#RS(sqA class that represents a thread of control. This class can be safely subclassed in a limited fashion. cCs|dkstdtj|||dkr=i}n||_t|pUt|_||_||_ |j |_ d|_ t |_t|_tt|_t|_tj|_dS(sCThis constructor should always be called with keyword arguments. Arguments are: *group* should be None; reserved for future extension when a ThreadGroup class is implemented. *target* is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called. *name* is the thread name. By default, a unique name is constructed of the form "Thread-N" where N is a small decimal number. *args* is the argument tuple for the target invocation. Defaults to (). *kwargs* is a dictionary of keyword arguments for the target invocation. Defaults to {}. If a subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.__init__()) before doing anything else to the thread. s#group argument must be None for nowN(RtAssertionErrorRR!t_Thread__targettstrRzt _Thread__namet _Thread__argst_Thread__kwargst _set_daemont_Thread__daemonict_Thread__identR t_Thread__startedRRt_Thread__stoppedRR t_Thread__blockRSt_Thread__initializedR&R't_Thread__stderr(RtgroupttargetR$R*R3R ((s!/usr/lib64/python2.7/threading.pyR!s         cCs0t|dr|jjn|jjdS(NR(thasattrRR!RRs(R((s!/usr/lib64/python2.7/threading.pyRsscCs|jS(N(R(R((s!/usr/lib64/python2.7/threading.pyt_blockscCs tjS(N(R tdaemon(R((s!/usr/lib64/python2.7/threading.pyRscCs|jstdd}|jjr3d}n|jrEd}n|jr[|d7}n|jdk r~|d|j7}nd|jj |j |fS(Ns Thread.__init__() was not calledtinitialtstartedtstoppeds daemons %ss <%s(%s, %s)>( RR{RRwRRRRR8R-R~(Rtstatus((s!/usr/lib64/python2.7/threading.pyR:s     cCs|jstdn|jjr6tdn|jd|t|t|twctlimitt_dequetqueue(RR((s!/usr/lib64/python2.7/threading.pyR!s    cSs|jjx9t|j|jkrH|jd||jjqW|jj||jd|t|j|j j |jj dS(Nsput(%s): queue fulls put(%s): appended, length now %d( RR;RPRRR,RRbRTR>RgR@(Rtitem((s!/usr/lib64/python2.7/threading.pytputs   cSs|jjx'|js6|jd|jjqW|jj}|jd|t|j|jj |jj |S(Nsget(): queue emptysget(): got %s, %d left( RR;RR,R>RbtpopleftRPRRgR@(RR((s!/usr/lib64/python2.7/threading.pytgets     (R-R.R!RR(((s!/usr/lib64/python2.7/threading.pyRs  tProducerThreadcBseZdZdZRS(cSs)tj|dd||_||_dS(NR$tProducer(RR!Rtquota(RRR((s!/usr/lib64/python2.7/threading.pyR!s cSshddlm}d}xK||jkrc|d}|jjd|j|ft|dqWdS(Ni(trandomiis%s.%dgh㈵>(RRRRR$RW(RRtcounter((s!/usr/lib64/python2.7/threading.pyRs  (R-R.R!R(((s!/usr/lib64/python2.7/threading.pyRs tConsumerThreadcBseZdZdZRS(cSs)tj|dd||_||_dS(NR$tConsumer(RR!RR(RRR((s!/usr/lib64/python2.7/threading.pyR!s cSs>x7|jdkr9|jj}|GH|jd|_qWdS(Nii(RRR(RR((s!/usr/lib64/python2.7/threading.pyRs(R-R.R!R(((s!/usr/lib64/python2.7/threading.pyR s iiis Producer-%digư>(RRtrangeR$RTRRWR( RRRtNPtQLtNItQtPtiRAtC((s!/usr/lib64/python2.7/threading.pyt_tests(       t__main__(NRItsysR&Rt ImportErrorRR-twarningst collectionsRRt itertoolsRt_countRRURRWt tracebackRRt__all__tstart_new_threadRt allocate_lockR4t get_identR"terrort ThreadErrortfilterwarningstDeprecationWarningRRRtobjectRRR/R1RRR R R2RRKRRjRRnR RptnextRxRzRR#RRRRRRRRR RRRR RRt _shutdownRRt_threading_localRR(((s!/usr/lib64/python2.7/threading.pyts             u  J  B            % R