^Rc@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(cCs"|dkrt}n||_dS(N(tNonet_VERBOSEt_Verbose__verbose(tselftverbose((s/usr/lib/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/lib/python2.7/threading.pyt_note?s    N(t__name__t __module__RR R+(((s/usr/lib/python2.7/threading.pyR8s 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/lib/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/lib/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(R)tkwargs((s/usr/lib/python2.7/threading.pyR qs R1cBsbeZdZd 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/lib/python2.7/threading.pyR s  cCsH|j}yt|j}Wntk r-nXd|jj||jfS(Ns<%s owner=%r count=%d>(R5R"R#R$t __class__R,R6(Rtowner((s/usr/lib/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!R5R6R+R4tacquire(Rtblockingtmetrc((s/usr/lib/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(R5R!t RuntimeErrorR6RR4treleaseR+(Rtcount((s/usr/lib/python2.7/threading.pyR?s  cCs|jdS(N(R?(Rtttvttb((s/usr/lib/python2.7/threading.pyt__exit__scCs?|\}}|jj||_||_|jd|dS(Ns%s._acquire_restore()(R4R:R6R5R+(Rt count_ownerR@R8((s/usr/lib/python2.7/threading.pyt_acquire_restores     cCsK|jd||j}d|_|j}d|_|jj||fS(Ns%s._release_save()i(R+R6R5RR4R?(RR@R8((s/usr/lib/python2.7/threading.pyt _release_saves     cCs|jtkS(N(R5R!(R((s/usr/lib/python2.7/threading.pyt _is_ownedsN( R,R-t__doc__RR R9R:t __enter__R?RDRFRGRH(((s/usr/lib/python2.7/threading.pyR1|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)R2((s/usr/lib/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/lib/python2.7/threading.pyR s&        cCs |jjS(N(RLRJ(R((s/usr/lib/python2.7/threading.pyRJscGs|jj|S(N(RLRD(RR)((s/usr/lib/python2.7/threading.pyRDscCsd|jt|jfS(Ns(RLtlenRN(R((s/usr/lib/python2.7/threading.pyR9"scCs|jjdS(N(RLR?(R((s/usr/lib/python2.7/threading.pyRG%scCs|jjdS(N(RLR:(Rtx((s/usr/lib/python2.7/threading.pyRF(scCs+|jjdr#|jjtStSdS(Ni(RLR:R?tFalsetTrue(R((s/usr/lib/python2.7/threading.pyRH+s c Csh|jstdnt}|j|jj||j}z |d krv|j|jd|nt |}d}xot r|jd}|rPn|t }|dkrPn|rt |d|d}n|}t |qW|s?|jd||y|jj |WqRtk r;qRXn|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>R3R:RNtappendRGRR+t_timeRStmint_sleeptremovet ValueErrorRF( Rttimeoutt balancingtwaitert saved_statetendtimetdelaytgotitt remaining((s/usr/lib/python2.7/threading.pytwait4s>           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/lib/python2.7/threading.pytnotifyws      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/lib/python2.7/threading.pyt notifyAllsN(R,R-RIRR RJRDR9RGRFRHRSRbRgRht notify_all(((s/usr/lib/python2.7/threading.pyRKs      C  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)R2((s/usr/lib/python2.7/threading.pyR s 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/lib/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/lib/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/lib/python2.7/threading.pyR?s    cCs|jdS(N(R?(RRARBRC((s/usr/lib/python2.7/threading.pyRDsN( R,R-RIRR R:RJR?RD(((s/usr/lib/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)R2((s/usr/lib/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/lib/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(RlRoRYRjR?(R((s/usr/lib/python2.7/threading.pyR?s N(R,R-RIRR R?(((s/usr/lib/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)R2((s/usr/lib/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/lib/python2.7/threading.pyR 1scCs|jjdS(N(RqR (R((s/usr/lib/python2.7/threading.pyt_reset_internal_locks6scCs|jS(s5Return true if and only if the internal flag is true.(Rr(R((s/usr/lib/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(RqR:RSRrRiR?(R((s/usr/lib/python2.7/threading.pytset@s   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(RqR:RRRrR?(R((s/usr/lib/python2.7/threading.pytclearNs  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(RqR:RrRbR?(RRZR[((s/usr/lib/python2.7/threading.pyRb[s    N( R,R-RIRR RsRttis_setRuRvRSRb(((s/usr/lib/python2.7/threading.pyRp(s     is Thread-%dcCstda|tS(Ni(t_counter(ttemplate((s/usr/lib/python2.7/threading.pyt_newnamets 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. 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)R2R((s/usr/lib/python2.7/threading.pyR s         cCs0t|dr|jjn|jjdS(NR(thasattrRR RRs(R((s/usr/lib/python2.7/threading.pyRsscCs|jS(N(R(R((s/usr/lib/python2.7/threading.pyt_blockscCs tjS(N(Rtdaemon(R((s/usr/lib/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{RRwRRRRR7R,R~(Rtstatus((s/usr/lib/python2.7/threading.pyR9s     cCs|jstdn|jjr6tdn|jd|t|t|RRwR+t_active_limbo_lockt_limbot_start_new_threadt_Thread__bootstrapt ExceptionRb(R((s/usr/lib/python2.7/threading.pytstarts   cCs@z&|jr%|j|j|jnWd|`|`|`XdS(sXMethod representing the thread's activity. You may override this method in a subclass. The standard run() method invokes the callable object passed to the object's constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively. N(R|RR(R((s/usr/lib/python2.7/threading.pytruns  cCs8y|jWn#|jr-tdkr-dSnXdS(N(t_Thread__bootstrap_innerRR%R(R((s/usr/lib/python2.7/threading.pyt __bootstraps cCst|_dS(N(R!R(R((s/usr/lib/python2.7/threading.pyt _set_identsc Cs z|j|jjt|t|jRRwRRR+RR:RRbRUR?(RRZR[tdeadlineR_((s/usr/lib/python2.7/threading.pytjoins.         cCs|jstd|jS(sA string used for identification purposes only. It has no semantics. Multiple threads may be given the same name. The initial name is set by the constructor. sThread.__init__() not called(RR{R~(R((s/usr/lib/python2.7/threading.pyR#scCs(|jstdt||_dS(NsThread.__init__() not called(RR{R}R~(RR#((s/usr/lib/python2.7/threading.pyR#scCs|jstd|jS(s;Thread identifier of this thread or None if it has not been started. This is a nonzero integer. See the thread.get_ident() function. Thread identifiers may be recycled when a thread exits and another thread is created. The identifier is available even after the thread has exited. sThread.__init__() not called(RR{R(R((s/usr/lib/python2.7/threading.pyR*s cCs,|jstd|jjo+|j S(sReturn whether the thread is alive. This method returns True just before the run() method starts until just after the run() method terminates. The module function enumerate() returns a list of all alive threads. sThread.__init__() not called(RR{RRwR(R((s/usr/lib/python2.7/threading.pytisAlivescCs|jstd|jS(sA boolean value indicating whether this thread is a daemon thread (True) or not (False). This must be set before start() is called, otherwise RuntimeError is raised. Its initial value is inherited from the creating thread; the main thread is not a daemon thread and therefore all threads created in the main thread default to daemon = False. The entire Python program exits when no alive non-daemon threads are left. sThread.__init__() not called(RR{R(R((s/usr/lib/python2.7/threading.pyRs cCsC|jstdn|jjr6tdn||_dS(NsThread.__init__() not calleds)cannot set daemon status of active thread(RR>RRwR(Rtdaemonic((s/usr/lib/python2.7/threading.pyRs  cCs|jS(N(R(R((s/usr/lib/python2.7/threading.pytisDaemon scCs ||_dS(N(R(RR((s/usr/lib/python2.7/threading.pyt setDaemon scCs|jS(N(R#(R((s/usr/lib/python2.7/threading.pytgetNamescCs ||_dS(N(R#(RR#((s/usr/lib/python2.7/threading.pytsetNamesN(($R,R-RIRRRR%texc_infoRt exc_clearRRR RstpropertyRRR9RRRRRRt_Thread__deleteRSRR#tsetterR*Rtis_aliveRRRRR(((s/usr/lib/python2.7/threading.pyRs:   (       J #8     cOs t||S(sFactory function to create a Timer object. Timers call a function after a specified number of seconds: t = Timer(30.0, f, args=[], kwargs={}) t.start() t.cancel() # stop the timer's action if it's still waiting (t_Timer(R)R2((s/usr/lib/python2.7/threading.pyRs RcBs/eZdZgidZdZdZRS(sCall a function after a specified number of seconds: t = Timer(30.0, f, args=[], kwargs={}) t.start() t.cancel() # stop the timer's action if it's still waiting cCsAtj|||_||_||_||_t|_dS(N(RR tintervaltfunctionR)R2R tfinished(RRRR)R2((s/usr/lib/python2.7/threading.pyR ,s      cCs|jjdS(s(Stop the timer if it hasn't finished yetN(RRu(R((s/usr/lib/python2.7/threading.pytcancel4scCsL|jj|j|jjs;|j|j|jn|jjdS(N(RRbRRwRR)R2Ru(R((s/usr/lib/python2.7/threading.pyR8s(R,R-RIR RR(((s/usr/lib/python2.7/threading.pyR#s t _MainThreadcBs#eZdZdZdZRS(cCsHtj|dd|jj|jt|tt(RRRRR#RW(RRtcounter((s/usr/lib/python2.7/threading.pyR s  (R,R-R R(((s/usr/lib/python2.7/threading.pyRs tConsumerThreadcBseZdZdZRS(cSs)tj|dd||_||_dS(NR#tConsumer(RR RR@(RRR@((s/usr/lib/python2.7/threading.pyR s cSs>x7|jdkr9|jj}|GH|jd|_qWdS(Nii(R@RR(RR((s/usr/lib/python2.7/threading.pyRs(R,R-R R(((s/usr/lib/python2.7/threading.pyRs iiis Producer-%digư>(RRtrangeR#RTRRWR( RRRtNPtQLtNItQtPtiRAtC((s/usr/lib/python2.7/threading.pyt_tests(       t__main__(JRItsysR%Rt ImportErrorRR,twarningst collectionsRRRRURRWt tracebackRRt__all__tstart_new_threadRt allocate_lockR3t get_identR!terrort ThreadErrortfilterwarningstDeprecationWarningRRRtobjectRRR.R0RRR R R1RRKR RjRRnR RpRxRzRR"RRRRRRRRRRRRR RRt _shutdownRRt_threading_localRR(((s/usr/lib/python2.7/threading.pyts             u  J  K           % R