nfc@s dZddlZyddlZWnek rBeje=nXddlZddlm Z ddl m Z m ZddlmZdddd d d d d dddddddddgZejZejZejZejZ[ejddeddddeZdefdYZ e!a"e!a#dZ$d Z%eZ&d!Z'd"e fd#YZ(d$Z)d%e fd&YZ*d'Z+d(e fd)YZ,d*Z-d+e,fd,YZ.d-Z/d.e fd/YZ0d0a1d1d2Z2ea3iZ4iZ5de fd3YZ6d4Z7d5e6fd6YZ8d7e6fd8YZ9d9Z:d:e6fd;YZ;d<Z<e<Z=d=Z>e>Z?d>Z@d?ZAdd@lmBZBe9jCZDyddAlmEZFWn!ek rddBlGmFZFnXdCZHdDZIedEkreIndS(Fs;Thread module emulating a subset of Java's threading model.iN(tdeque(ttimetsleep(t format_exct activeCountt active_countt Conditiont currentThreadtcurrent_threadt enumeratetEventtLocktRLockt SemaphoretBoundedSemaphoretThreadtTimert setprofiletsettracetlocalt stack_sizetignoretcategorytmodulet threadingtmessages sys.exc_cleart_VerbosecBseZddZdZRS(cCsdS(N((tselftverbose((s!/usr/lib64/python2.7/threading.pyt__init__OscGsdS(N((Rtargs((s!/usr/lib64/python2.7/threading.pyt_noteQsN(t__name__t __module__tNoneRR(((s!/usr/lib64/python2.7/threading.pyRNs 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.pyRYscCs |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(R$((s!/usr/lib64/python2.7/threading.pyRcscOs 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(Rtkwargs((s!/usr/lib64/python2.7/threading.pyR qs R&cBsbeZdZd 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(RRt_allocate_lockt _RLock__blockR"t _RLock__ownert _RLock__count(RR((s!/usr/lib64/python2.7/threading.pyRs  cCsH|j}yt|j}Wntk r-nXd|jj||jfS(Ns<%s owner=%r count=%d>(R*t_activetnametKeyErrort __class__R R+(Rtowner((s!/usr/lib64/python2.7/threading.pyt__repr__s  icCs]t}|j|kr,|jd|_dS|jj|}|rY||_d|_n|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. i(t _get_identR*R+R)tacquire(Rtblockingtmetrc((s!/usr/lib64/python2.7/threading.pyR3s   cCsX|jtkr!tdn|jd|_}|sTd|_|jjndS(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 lockiN(R*R2t RuntimeErrorR+R"R)trelease(Rtcount((s!/usr/lib64/python2.7/threading.pyR8s  cCs|jdS(N(R8(Rtttvttb((s!/usr/lib64/python2.7/threading.pyt__exit__scCs/|\}}|jj||_||_dS(N(R)R3R+R*(Rt count_ownerR9R0((s!/usr/lib64/python2.7/threading.pyt_acquire_restores     cCs;|j}d|_|j}d|_|jj||fS(Ni(R+R*R"R)R8(RR9R0((s!/usr/lib64/python2.7/threading.pyt _release_saves      cCs|jtkS(N(R*R2(R((s!/usr/lib64/python2.7/threading.pyt _is_ownedsN( R R!t__doc__R"RR1R3t __enter__R8R=R?R@RA(((s!/usr/lib64/python2.7/threading.pyR&|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(RR'((s!/usr/lib64/python2.7/threading.pyRs RDcBs}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( RRR"R t_Condition__lockR3R8R@tAttributeErrorR?RAt_Condition__waiters(RtlockR((s!/usr/lib64/python2.7/threading.pyRs&        cCs |jjS(N(RERC(R((s!/usr/lib64/python2.7/threading.pyRCscGs|jj|S(N(RER=(RR((s!/usr/lib64/python2.7/threading.pyR=scCsd|jt|jfS(Ns(REtlenRG(R((s!/usr/lib64/python2.7/threading.pyR1"scCs|jjdS(N(RER8(R((s!/usr/lib64/python2.7/threading.pyR@%scCs|jjdS(N(RER3(Rtx((s!/usr/lib64/python2.7/threading.pyR?(scCs+|jjdr#|jjtStSdS(Ni(RER3R8tFalsetTrue(R((s!/usr/lib64/python2.7/threading.pyRA+s c CsH|jstdnt}|j|jj||j}z|dkrf|jnt|}d}xt r|jd}|rPnt |t|}|dkrPn|rt |d|d}n|}t|}t |q|W|s2y|jj |Wq2t k r.q2XnWd|j|XdS(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 lockgMb@?iig?N(RAR7R(R3RGtappendR@R"t_timeRLtmint_sleeptremovet ValueErrorR?( Rttimeoutt balancingtwaitert saved_statetendtimetdelaytgotitt remaining((s!/usr/lib64/python2.7/threading.pytwait4s>           icCs|jstdn|j}|| }|s8dS|jd|||dkrYdp\dx=|D]5}|jy|j|Wqgtk rqgXqgWdS(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 lockNs"%s.notify(): notifying %d waiter%sitst(RAR7RGRR8RQRR(RtnRGtwaitersRU((s!/usr/lib64/python2.7/threading.pytnotifyxs      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(R`RIRG(R((s!/usr/lib64/python2.7/threading.pyt notifyAllsN(R R!RBR"RRCR=R1R@R?RARLR[R`Rat notify_all(((s!/usr/lib64/python2.7/threading.pyRDs      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(RR'((s!/usr/lib64/python2.7/threading.pyR s RccBsAeZdZdddZddZeZdZdZRS(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(RRRRRR t_Semaphore__condt_Semaphore__value(RtvalueR((s!/usr/lib64/python2.7/threading.pyRs  cCs]t}|jHx@|jdkr<|s,Pn|jjqW|jd|_t}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. iiN(RKRdReR[RL(RR4R6((s!/usr/lib64/python2.7/threading.pyR3s  cCs1|j"|jd|_|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. iN(RdReR`(R((s!/usr/lib64/python2.7/threading.pyR8s cCs|jdS(N(R8(RR:R;R<((s!/usr/lib64/python2.7/threading.pyR=sN( R R!RBR"RR3RCR8R=(((s!/usr/lib64/python2.7/threading.pyRcs  % 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(RR'((s!/usr/lib64/python2.7/threading.pyRsRgcBs&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(RcRt_initial_value(RRfR((s!/usr/lib64/python2.7/threading.pyR scCs.|j|jkr!tdntj|S(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 times(ReRhRRRcR8(R((s!/usr/lib64/python2.7/threading.pyR8s N(R R!RBR"RR8(((s!/usr/lib64/python2.7/threading.pyRgscOs 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(RR'((s!/usr/lib64/python2.7/threading.pyR sRicBsSeZdZddZdZdZeZdZdZ 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(RRRR t _Event__condRKt _Event__flag(RR((s!/usr/lib64/python2.7/threading.pyR2scCs|jjdS(N(RjR(R((s!/usr/lib64/python2.7/threading.pyt_reset_internal_locks7scCs|jS(s5Return true if and only if the internal flag is true.(Rk(R((s!/usr/lib64/python2.7/threading.pytisSet;scCs<|jjzt|_|jjWd|jjXdS(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(RjR3RLRkRbR8(R((s!/usr/lib64/python2.7/threading.pytsetAs   cCs/|jjz t|_Wd|jjXdS(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(RjR3RKRkR8(R((s!/usr/lib64/python2.7/threading.pytclearOs  cCsL|jjz*|js/|jj||n|jSWd|jjXdS(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(RjR3RkR[R8(RRSRT((s!/usr/lib64/python2.7/threading.pyR[\s    N( R R!RBR"RRlRmtis_setRnRoRLR[(((s!/usr/lib64/python2.7/threading.pyRi)s     is Thread-%dcCstda|tS(Ni(t_counter(ttemplate((s!/usr/lib64/python2.7/threading.pyt_newnameus cBs=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. cCstj|||dkr%i}n||_t|p=t|_||_||_|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. N(RRR"t_Thread__targettstrRst _Thread__namet _Thread__argst_Thread__kwargst _set_daemont_Thread__daemonict_Thread__identR t_Thread__startedRKt_Thread__stoppedRR t_Thread__blockRLt_Thread__initializedt_syststderrt_Thread__stderr(RtgroupttargetR-RR'R((s!/usr/lib64/python2.7/threading.pyRs         cCs0t|dr|jjn|jjdS(NR~(thasattrR~RR|Rl(R((s!/usr/lib64/python2.7/threading.pyRlscCs|jS(N(R~(R((s!/usr/lib64/python2.7/threading.pyt_blockscCs tjS(N(Rtdaemon(R((s!/usr/lib64/python2.7/threading.pyRyscCsd}|jjrd}n|jr0d}n|jrF|d7}n|jdk ri|d|j7}nd|jj|j|fS(Ntinitialtstartedtstoppeds daemons %ss <%s(%s, %s)>( R|RpR}RzR{R"R/R Rv(Rtstatus((s!/usr/lib64/python2.7/threading.pyR1s     cCs|jstdn|jjr6tdnt|t|(RRRRR-RP(RRtcounter((s!/usr/lib64/python2.7/threading.pyR s  (R R!RR(((s!/usr/lib64/python2.7/threading.pyRs tConsumerThreadcBseZdZdZRS(cSs)tj|dd||_||_dS(NR-tConsumer(RRRR9(RRR9((s!/usr/lib64/python2.7/threading.pyRs cSs>x7|jdkr9|jj}|GH|jd|_qWdS(Nii(R9RR(RR((s!/usr/lib64/python2.7/threading.pyRs(R R!RR(((s!/usr/lib64/python2.7/threading.pyRs iiis Producer-%digư>(RRtrangeR-RMRRPR( RRRtNPtQLtNItQtPtiR:tC((s!/usr/lib64/python2.7/threading.pyt_tests(       t__main__(JRBtsysRRt ImportErrorRR twarningst collectionsRRRRNRRPt tracebackRRt__all__tstart_new_threadRt allocate_lockR(t get_identR2terrort ThreadErrortfilterwarningstDeprecationWarningRKt_VERBOSEtobjectRR"R#R%RRR R R&RRDR RcRRgR RiRqRsRR,RRRRRRRRRRRRR RRt _shutdownRRt_threading_localRR(((s!/usr/lib64/python2.7/threading.pyts             u  J  K           % R