î j f9ã@südZdddddgZddlZdd lmZdd lmZdd lmZdd lmZGd d„dƒZ Gdd„dƒZ Gdd„de ƒZ Gdd„dƒZ Gdd„de ƒZ Gdd„de ƒZGdd„deƒZdS)zSynchronization primitives.ÚLockÚEventÚ ConditionÚ SemaphoreÚBoundedSemaphoreéNé)Úcompat)Úevents)Úfutures)Ú coroutinec@s:eZdZdZdd„Zdd„Zdd„ZdS) Ú_ContextManageraContext manager. This enables the following idiom for acquiring and releasing a lock around a block: with (yield from lock): while failing loudly when accidentally using: with lock: cCs ||_dS)N)Ú_lock)ÚselfÚlock©rú2/opt/alt/python34/lib64/python3.4/asyncio/locks.pyÚ__init__sz_ContextManager.__init__cCsdS)Nr)rrrrÚ __enter__sz_ContextManager.__enter__c Gs"z|jjƒWdd|_XdS)N)r Úrelease)rÚargsrrrÚ__exit__$sz_ContextManager.__exit__N)Ú__name__Ú __module__Ú __qualname__Ú__doc__rrrrrrrr s   r c@sveZdZdd„Zdd„Zedd„ƒZejrrdd„Z ed d „ƒZ ed d „ƒZ nd S)Ú_ContextManagerMixincCstdƒ‚dS)Nz9"yield from" should be used as context manager expression)Ú RuntimeError)rrrrr,sz_ContextManagerMixin.__enter__cGsdS)Nr)rrrrrr0sz_ContextManagerMixin.__exit__ccs|jƒDdHt|ƒS)N)Úacquirer )rrrrÚ__iter__5sz_ContextManagerMixin.__iter__ccs|jƒDdHt|ƒS)N)rr )rrrrÚ __await__Hsz_ContextManagerMixin.__await__ccs|jƒDdHdS)N)r)rrrrÚ __aenter__Msz_ContextManagerMixin.__aenter__cCs|jƒdS)N)r)rÚexc_typeÚexcÚtbrrrÚ __aexit__Tsz_ContextManagerMixin.__aexit__N) rrrrrr rrZPY35rr r$rrrrr+s     rcsdeZdZdZdddd„Z‡fdd†Zdd „Zed d „ƒZd d „Z ‡S)ra›Primitive lock objects. A primitive lock is a synchronization primitive that is not owned by a particular coroutine when locked. A primitive lock is in one of two states, 'locked' or 'unlocked'. It is created in the unlocked state. It has two basic methods, acquire() and release(). When the state is unlocked, acquire() changes the state to locked and returns immediately. When the state is locked, acquire() blocks until a call to release() in another coroutine changes it to unlocked, then the acquire() call resets it to locked and returns. The release() method should only be called in the locked state; it changes the state to unlocked and returns immediately. If an attempt is made to release an unlocked lock, a RuntimeError will be raised. When more than one coroutine is blocked in acquire() waiting for the state to turn to unlocked, only one coroutine proceeds when a release() call resets the state to unlocked; first coroutine which is blocked in acquire() is being processed. acquire() is a coroutine and should be called with 'yield from'. Locks also support the context management protocol. '(yield from lock)' should be used as context manager expression. Usage: lock = Lock() ... yield from lock try: ... finally: lock.release() Context manager usage: lock = Lock() ... with (yield from lock): ... Lock objects can be tested for locking state: if not lock.locked(): yield from lock else: # lock is acquired ... ÚloopNcCsCtjƒ|_d|_|dk r0||_ntjƒ|_dS)NF)Ú collectionsÚdequeÚ_waitersÚ_lockedÚ_loopr Úget_event_loop)rr%rrrr�s    z Lock.__init__csetƒjƒ}|jrdnd}|jrKdj|t|jƒƒ}ndj|dd…|ƒS)NÚlockedÚunlockedz {},waiters:{}z <{} [{}]>réÿÿÿÿ)ÚsuperÚ__repr__r)r(ÚformatÚlen)rÚresÚextra)Ú __class__rrr0—s  z Lock.__repr__cCs|jS)z Return True if lock is acquired.)r))rrrrr,žsz Lock.lockedc csx|j r!|j r!d|_dStjd|jƒ}|jj|ƒz|DdHd|_dSWd|jj|ƒXdS)z�Acquire a lock. This method blocks until the lock is unlocked, then sets it to locked and returns True. Tr%N)r(r)r ÚFuturer*ÚappendÚremove)rÚfutrrrr¢s   z Lock.acquirecCsV|jrFd|_x=|jD]#}|jƒs|jdƒPqqWn tdƒ‚dS)aGRelease a lock. When the lock is locked, reset it to unlocked, and return. If any other coroutines are blocked waiting for the lock to become unlocked, allow exactly one of them to proceed. When invoked on an unlocked lock, a RuntimeError is raised. There is no return value. FTzLock is not acquired.N)r)r(ÚdoneÚ set_resultr)rr9rrrr¶s     z Lock.release) rrrrrr0r,r rrrr)r5rrYs 4 cspeZdZdZdddd„Z‡fdd†Zdd „Zd d „Zd d „Ze dd„ƒZ ‡S)ra#Asynchronous equivalent to threading.Event. Class implementing event objects. 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. The flag is initially false. r%NcCsCtjƒ|_d|_|dk r0||_ntjƒ|_dS)NF)r&r'r(Ú_valuer*r r+)rr%rrrrÕs    zEvent.__init__csetƒjƒ}|jrdnd}|jrKdj|t|jƒƒ}ndj|dd…|ƒS)NÚsetZunsetz {},waiters:{}z <{} [{}]>rr.)r/r0r<r(r1r2)rr3r4)r5rrr0Ýs  zEvent.__repr__cCs|jS)z5Return True if and only if the internal flag is true.)r<)rrrrÚis_setäsz Event.is_setcCsI|jsEd|_x0|jD]"}|jƒs|jdƒqqWndS)zºSet the internal flag to true. All coroutines waiting for it to become true are awakened. Coroutine that call wait() once the flag is true will not block at all. TN)r<r(r:r;)rr9rrrr=ès    z Event.setcCs d|_dS)z¢Reset the internal flag to false. Subsequently, coroutines calling wait() will block until set() is called to set the internal flag to true again.FN)r<)rrrrÚclearôsz Event.clearc cs[|jr dStjd|jƒ}|jj|ƒz|DdHdSWd|jj|ƒXdS)zéBlock until the internal flag is true. If the internal flag is true on entry, return True immediately. Otherwise, block until another coroutine calls set() to set the flag to true, then return True. Tr%N)r<r r6r*r(r7r8)rr9rrrÚwaitús  z Event.wait) rrrrrr0r>r=r?r r@rr)r5rrÌs   cs|eZdZdZddddd„Z‡fdd†Zedd „ƒZed d „ƒZd d d„Z dd„Z ‡S)raAsynchronous equivalent to threading.Condition. This class implements condition variable objects. A condition variable allows one or more coroutines to wait until they are notified by another coroutine. A new Lock object is created and used as the underlying lock. Nr%cCs©|dk r||_ntjƒ|_|dkrHtd|jƒ}n!|j|jk ritdƒ‚n||_|j|_|j|_|j|_t j ƒ|_ dS)Nr%z"loop argument must agree with lock) r*r r+rÚ ValueErrorr r,rrr&r'r()rrr%rrrrs       zCondition.__init__cshtƒjƒ}|jƒr!dnd}|jrNdj|t|jƒƒ}ndj|dd…|ƒS)Nr,r-z {},waiters:{}z <{} [{}]>rr.)r/r0r,r(r1r2)rr3r4)r5rrr0+s  zCondition.__repr__ccsŠ|jƒstdƒ‚n|jƒzNtjd|jƒ}|jj|ƒz|DdHdSWd|jj|ƒXWd|j ƒDdHXdS)ašWait until notified. If the calling coroutine 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 notify_all() call for the same condition variable in another coroutine. Once awakened, it re-acquires the lock and returns True. zcannot wait on un-acquired lockr%NT) r,rrr r6r*r(r7r8r)rr9rrrr@2s   zCondition.waitccs2|ƒ}x"|s-|jƒDdH|ƒ}q W|S)zÎWait until a predicate becomes true. The predicate should be a callable which result will be interpreted as a boolean value. The final predicate value is the return value. N)r@)rZ predicateÚresultrrrÚwait_forNs    zCondition.wait_forrcCso|jƒstdƒ‚nd}xG|jD]<}||krAPn|jƒs+|d7}|jdƒq+q+WdS)aBy default, wake up one coroutine waiting on this condition, if any. If the calling coroutine has not acquired the lock when this method is called, a RuntimeError is raised. This method wakes up at most n of the coroutines waiting for the condition variable; it is a no-op if no coroutines are waiting. Note: an awakened coroutine does not actually return from its wait() call until it can reacquire the lock. Since notify() does not release the lock, its caller should. z!cannot notify on un-acquired lockrrFN)r,rr(r:r;)rÚnÚidxr9rrrÚnotify\s    zCondition.notifycCs|jt|jƒƒdS)aWake up all threads waiting on this condition. This method acts like notify(), but wakes up all waiting threads instead of one. If the calling thread has not acquired the lock when this method is called, a RuntimeError is raised. N)rFr2r()rrrrÚ notify_alltszCondition.notify_all) rrrrrr0r r@rCrFrGrr)r5rrs csseZdZdZddddd„Z‡fdd†Zd d „Zd d „Zed d„ƒZ dd„Z ‡S)raA Semaphore implementation. A semaphore manages an internal counter which is decremented by each acquire() call and incremented by each release() call. The counter can never go below zero; when acquire() finds that it is zero, it blocks, waiting until some other thread calls release(). Semaphores also support the context management protocol. The optional argument gives the initial value for the internal counter; it defaults to 1. If the value given is less than 0, ValueError is raised. rr%NcCs^|dkrtdƒ‚n||_tjƒ|_|dk rK||_ntjƒ|_dS)Nrz$Semaphore initial value must be >= 0)rAr<r&r'r(r*r r+)rÚvaluer%rrrrŒs    zSemaphore.__init__csttƒjƒ}|jƒr!dndj|jƒ}|jrZdj|t|jƒƒ}ndj|dd…|ƒS)Nr,zunlocked,value:{}z {},waiters:{}z <{} [{}]>rr.)r/r0r,r1r<r(r2)rr3r4)r5rrr0–s   zSemaphore.__repr__cCs@x9|jr;|jjƒ}|jƒs|jdƒdSqWdS)N)r(Úpopleftr:r;)rZwaiterrrrÚ _wake_up_nextžs    zSemaphore._wake_up_nextcCs |jdkS)z:Returns True if semaphore can not be acquired immediately.r)r<)rrrrr,¥szSemaphore.lockedc csœx†|jdkrˆtjd|jƒ}|jj|ƒy |DdHWq|jƒ|jdkr}|jƒ r}|jƒn‚YqXqW|jd8_dS)a5Acquire a semaphore. If the internal counter is larger than zero on entry, decrement it by one and return True immediately. If it is zero on entry, block, waiting until some other coroutine has called release() to make it larger than 0, and then return True. rr%NrT) r<r r6r*r(r7ZcancelZ cancelledrJ)rr9rrrr©s     zSemaphore.acquirecCs|jd7_|jƒdS)zÓRelease a semaphore, incrementing the internal counter by one. When it was zero on entry and another coroutine is waiting for it to become larger than zero again, wake up that coroutine. rN)r<rJ)rrrrrÁszSemaphore.release) rrrrrr0rJr,r rrrr)r5rr}s    csCeZdZdZddd‡fdd†Z‡fdd†Z‡S) rz�A bounded semaphore implementation. This raises ValueError in release() if it would increase the value above the initial value. rr%Ncs#||_tƒj|d|ƒdS)Nr%)Ú _bound_valuer/r)rrHr%)r5rrrÑs zBoundedSemaphore.__init__cs2|j|jkr!tdƒ‚ntƒjƒdS)Nz(BoundedSemaphore released too many times)r<rKrAr/r)r)r5rrrÕszBoundedSemaphore.release)rrrrrrrr)r5rrÊs )rÚ__all__r&Úrr r Z coroutinesr r rrrrrrrrrrÚs .sBoM