Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3..../asyncio
File: locks.py
"""Synchronization primitives."""
[0] Fix | Delete
[1] Fix | Delete
__all__ = ['Lock', 'Event', 'Condition', 'Semaphore', 'BoundedSemaphore']
[2] Fix | Delete
[3] Fix | Delete
import collections
[4] Fix | Delete
[5] Fix | Delete
from . import compat
[6] Fix | Delete
from . import events
[7] Fix | Delete
from . import futures
[8] Fix | Delete
from .coroutines import coroutine
[9] Fix | Delete
[10] Fix | Delete
[11] Fix | Delete
class _ContextManager:
[12] Fix | Delete
"""Context manager.
[13] Fix | Delete
[14] Fix | Delete
This enables the following idiom for acquiring and releasing a
[15] Fix | Delete
lock around a block:
[16] Fix | Delete
[17] Fix | Delete
with (yield from lock):
[18] Fix | Delete
<block>
[19] Fix | Delete
[20] Fix | Delete
while failing loudly when accidentally using:
[21] Fix | Delete
[22] Fix | Delete
with lock:
[23] Fix | Delete
<block>
[24] Fix | Delete
"""
[25] Fix | Delete
[26] Fix | Delete
def __init__(self, lock):
[27] Fix | Delete
self._lock = lock
[28] Fix | Delete
[29] Fix | Delete
def __enter__(self):
[30] Fix | Delete
# We have no use for the "as ..." clause in the with
[31] Fix | Delete
# statement for locks.
[32] Fix | Delete
return None
[33] Fix | Delete
[34] Fix | Delete
def __exit__(self, *args):
[35] Fix | Delete
try:
[36] Fix | Delete
self._lock.release()
[37] Fix | Delete
finally:
[38] Fix | Delete
self._lock = None # Crudely prevent reuse.
[39] Fix | Delete
[40] Fix | Delete
[41] Fix | Delete
class _ContextManagerMixin:
[42] Fix | Delete
def __enter__(self):
[43] Fix | Delete
raise RuntimeError(
[44] Fix | Delete
'"yield from" should be used as context manager expression')
[45] Fix | Delete
[46] Fix | Delete
def __exit__(self, *args):
[47] Fix | Delete
# This must exist because __enter__ exists, even though that
[48] Fix | Delete
# always raises; that's how the with-statement works.
[49] Fix | Delete
pass
[50] Fix | Delete
[51] Fix | Delete
@coroutine
[52] Fix | Delete
def __iter__(self):
[53] Fix | Delete
# This is not a coroutine. It is meant to enable the idiom:
[54] Fix | Delete
#
[55] Fix | Delete
# with (yield from lock):
[56] Fix | Delete
# <block>
[57] Fix | Delete
#
[58] Fix | Delete
# as an alternative to:
[59] Fix | Delete
#
[60] Fix | Delete
# yield from lock.acquire()
[61] Fix | Delete
# try:
[62] Fix | Delete
# <block>
[63] Fix | Delete
# finally:
[64] Fix | Delete
# lock.release()
[65] Fix | Delete
yield from self.acquire()
[66] Fix | Delete
return _ContextManager(self)
[67] Fix | Delete
[68] Fix | Delete
if compat.PY35:
[69] Fix | Delete
[70] Fix | Delete
def __await__(self):
[71] Fix | Delete
# To make "with await lock" work.
[72] Fix | Delete
yield from self.acquire()
[73] Fix | Delete
return _ContextManager(self)
[74] Fix | Delete
[75] Fix | Delete
@coroutine
[76] Fix | Delete
def __aenter__(self):
[77] Fix | Delete
yield from self.acquire()
[78] Fix | Delete
# We have no use for the "as ..." clause in the with
[79] Fix | Delete
# statement for locks.
[80] Fix | Delete
return None
[81] Fix | Delete
[82] Fix | Delete
@coroutine
[83] Fix | Delete
def __aexit__(self, exc_type, exc, tb):
[84] Fix | Delete
self.release()
[85] Fix | Delete
[86] Fix | Delete
[87] Fix | Delete
class Lock(_ContextManagerMixin):
[88] Fix | Delete
"""Primitive lock objects.
[89] Fix | Delete
[90] Fix | Delete
A primitive lock is a synchronization primitive that is not owned
[91] Fix | Delete
by a particular coroutine when locked. A primitive lock is in one
[92] Fix | Delete
of two states, 'locked' or 'unlocked'.
[93] Fix | Delete
[94] Fix | Delete
It is created in the unlocked state. It has two basic methods,
[95] Fix | Delete
acquire() and release(). When the state is unlocked, acquire()
[96] Fix | Delete
changes the state to locked and returns immediately. When the
[97] Fix | Delete
state is locked, acquire() blocks until a call to release() in
[98] Fix | Delete
another coroutine changes it to unlocked, then the acquire() call
[99] Fix | Delete
resets it to locked and returns. The release() method should only
[100] Fix | Delete
be called in the locked state; it changes the state to unlocked
[101] Fix | Delete
and returns immediately. If an attempt is made to release an
[102] Fix | Delete
unlocked lock, a RuntimeError will be raised.
[103] Fix | Delete
[104] Fix | Delete
When more than one coroutine is blocked in acquire() waiting for
[105] Fix | Delete
the state to turn to unlocked, only one coroutine proceeds when a
[106] Fix | Delete
release() call resets the state to unlocked; first coroutine which
[107] Fix | Delete
is blocked in acquire() is being processed.
[108] Fix | Delete
[109] Fix | Delete
acquire() is a coroutine and should be called with 'yield from'.
[110] Fix | Delete
[111] Fix | Delete
Locks also support the context management protocol. '(yield from lock)'
[112] Fix | Delete
should be used as the context manager expression.
[113] Fix | Delete
[114] Fix | Delete
Usage:
[115] Fix | Delete
[116] Fix | Delete
lock = Lock()
[117] Fix | Delete
...
[118] Fix | Delete
yield from lock
[119] Fix | Delete
try:
[120] Fix | Delete
...
[121] Fix | Delete
finally:
[122] Fix | Delete
lock.release()
[123] Fix | Delete
[124] Fix | Delete
Context manager usage:
[125] Fix | Delete
[126] Fix | Delete
lock = Lock()
[127] Fix | Delete
...
[128] Fix | Delete
with (yield from lock):
[129] Fix | Delete
...
[130] Fix | Delete
[131] Fix | Delete
Lock objects can be tested for locking state:
[132] Fix | Delete
[133] Fix | Delete
if not lock.locked():
[134] Fix | Delete
yield from lock
[135] Fix | Delete
else:
[136] Fix | Delete
# lock is acquired
[137] Fix | Delete
...
[138] Fix | Delete
[139] Fix | Delete
"""
[140] Fix | Delete
[141] Fix | Delete
def __init__(self, *, loop=None):
[142] Fix | Delete
self._waiters = collections.deque()
[143] Fix | Delete
self._locked = False
[144] Fix | Delete
if loop is not None:
[145] Fix | Delete
self._loop = loop
[146] Fix | Delete
else:
[147] Fix | Delete
self._loop = events.get_event_loop()
[148] Fix | Delete
[149] Fix | Delete
def __repr__(self):
[150] Fix | Delete
res = super().__repr__()
[151] Fix | Delete
extra = 'locked' if self._locked else 'unlocked'
[152] Fix | Delete
if self._waiters:
[153] Fix | Delete
extra = '{},waiters:{}'.format(extra, len(self._waiters))
[154] Fix | Delete
return '<{} [{}]>'.format(res[1:-1], extra)
[155] Fix | Delete
[156] Fix | Delete
def locked(self):
[157] Fix | Delete
"""Return True if lock is acquired."""
[158] Fix | Delete
return self._locked
[159] Fix | Delete
[160] Fix | Delete
@coroutine
[161] Fix | Delete
def acquire(self):
[162] Fix | Delete
"""Acquire a lock.
[163] Fix | Delete
[164] Fix | Delete
This method blocks until the lock is unlocked, then sets it to
[165] Fix | Delete
locked and returns True.
[166] Fix | Delete
"""
[167] Fix | Delete
if not self._locked and all(w.cancelled() for w in self._waiters):
[168] Fix | Delete
self._locked = True
[169] Fix | Delete
return True
[170] Fix | Delete
[171] Fix | Delete
fut = self._loop.create_future()
[172] Fix | Delete
self._waiters.append(fut)
[173] Fix | Delete
[174] Fix | Delete
# Finally block should be called before the CancelledError
[175] Fix | Delete
# handling as we don't want CancelledError to call
[176] Fix | Delete
# _wake_up_first() and attempt to wake up itself.
[177] Fix | Delete
try:
[178] Fix | Delete
try:
[179] Fix | Delete
yield from fut
[180] Fix | Delete
finally:
[181] Fix | Delete
self._waiters.remove(fut)
[182] Fix | Delete
except futures.CancelledError:
[183] Fix | Delete
if not self._locked:
[184] Fix | Delete
self._wake_up_first()
[185] Fix | Delete
raise
[186] Fix | Delete
[187] Fix | Delete
self._locked = True
[188] Fix | Delete
return True
[189] Fix | Delete
[190] Fix | Delete
def release(self):
[191] Fix | Delete
"""Release a lock.
[192] Fix | Delete
[193] Fix | Delete
When the lock is locked, reset it to unlocked, and return.
[194] Fix | Delete
If any other coroutines are blocked waiting for the lock to become
[195] Fix | Delete
unlocked, allow exactly one of them to proceed.
[196] Fix | Delete
[197] Fix | Delete
When invoked on an unlocked lock, a RuntimeError is raised.
[198] Fix | Delete
[199] Fix | Delete
There is no return value.
[200] Fix | Delete
"""
[201] Fix | Delete
if self._locked:
[202] Fix | Delete
self._locked = False
[203] Fix | Delete
self._wake_up_first()
[204] Fix | Delete
else:
[205] Fix | Delete
raise RuntimeError('Lock is not acquired.')
[206] Fix | Delete
[207] Fix | Delete
def _wake_up_first(self):
[208] Fix | Delete
"""Wake up the first waiter if it isn't done."""
[209] Fix | Delete
try:
[210] Fix | Delete
fut = next(iter(self._waiters))
[211] Fix | Delete
except StopIteration:
[212] Fix | Delete
return
[213] Fix | Delete
[214] Fix | Delete
# .done() necessarily means that a waiter will wake up later on and
[215] Fix | Delete
# either take the lock, or, if it was cancelled and lock wasn't
[216] Fix | Delete
# taken already, will hit this again and wake up a new waiter.
[217] Fix | Delete
if not fut.done():
[218] Fix | Delete
fut.set_result(True)
[219] Fix | Delete
[220] Fix | Delete
[221] Fix | Delete
class Event:
[222] Fix | Delete
"""Asynchronous equivalent to threading.Event.
[223] Fix | Delete
[224] Fix | Delete
Class implementing event objects. An event manages a flag that can be set
[225] Fix | Delete
to true with the set() method and reset to false with the clear() method.
[226] Fix | Delete
The wait() method blocks until the flag is true. The flag is initially
[227] Fix | Delete
false.
[228] Fix | Delete
"""
[229] Fix | Delete
[230] Fix | Delete
def __init__(self, *, loop=None):
[231] Fix | Delete
self._waiters = collections.deque()
[232] Fix | Delete
self._value = False
[233] Fix | Delete
if loop is not None:
[234] Fix | Delete
self._loop = loop
[235] Fix | Delete
else:
[236] Fix | Delete
self._loop = events.get_event_loop()
[237] Fix | Delete
[238] Fix | Delete
def __repr__(self):
[239] Fix | Delete
res = super().__repr__()
[240] Fix | Delete
extra = 'set' if self._value else 'unset'
[241] Fix | Delete
if self._waiters:
[242] Fix | Delete
extra = '{},waiters:{}'.format(extra, len(self._waiters))
[243] Fix | Delete
return '<{} [{}]>'.format(res[1:-1], extra)
[244] Fix | Delete
[245] Fix | Delete
def is_set(self):
[246] Fix | Delete
"""Return True if and only if the internal flag is true."""
[247] Fix | Delete
return self._value
[248] Fix | Delete
[249] Fix | Delete
def set(self):
[250] Fix | Delete
"""Set the internal flag to true. All coroutines waiting for it to
[251] Fix | Delete
become true are awakened. Coroutine that call wait() once the flag is
[252] Fix | Delete
true will not block at all.
[253] Fix | Delete
"""
[254] Fix | Delete
if not self._value:
[255] Fix | Delete
self._value = True
[256] Fix | Delete
[257] Fix | Delete
for fut in self._waiters:
[258] Fix | Delete
if not fut.done():
[259] Fix | Delete
fut.set_result(True)
[260] Fix | Delete
[261] Fix | Delete
def clear(self):
[262] Fix | Delete
"""Reset the internal flag to false. Subsequently, coroutines calling
[263] Fix | Delete
wait() will block until set() is called to set the internal flag
[264] Fix | Delete
to true again."""
[265] Fix | Delete
self._value = False
[266] Fix | Delete
[267] Fix | Delete
@coroutine
[268] Fix | Delete
def wait(self):
[269] Fix | Delete
"""Block until the internal flag is true.
[270] Fix | Delete
[271] Fix | Delete
If the internal flag is true on entry, return True
[272] Fix | Delete
immediately. Otherwise, block until another coroutine calls
[273] Fix | Delete
set() to set the flag to true, then return True.
[274] Fix | Delete
"""
[275] Fix | Delete
if self._value:
[276] Fix | Delete
return True
[277] Fix | Delete
[278] Fix | Delete
fut = self._loop.create_future()
[279] Fix | Delete
self._waiters.append(fut)
[280] Fix | Delete
try:
[281] Fix | Delete
yield from fut
[282] Fix | Delete
return True
[283] Fix | Delete
finally:
[284] Fix | Delete
self._waiters.remove(fut)
[285] Fix | Delete
[286] Fix | Delete
[287] Fix | Delete
class Condition(_ContextManagerMixin):
[288] Fix | Delete
"""Asynchronous equivalent to threading.Condition.
[289] Fix | Delete
[290] Fix | Delete
This class implements condition variable objects. A condition variable
[291] Fix | Delete
allows one or more coroutines to wait until they are notified by another
[292] Fix | Delete
coroutine.
[293] Fix | Delete
[294] Fix | Delete
A new Lock object is created and used as the underlying lock.
[295] Fix | Delete
"""
[296] Fix | Delete
[297] Fix | Delete
def __init__(self, lock=None, *, loop=None):
[298] Fix | Delete
if loop is not None:
[299] Fix | Delete
self._loop = loop
[300] Fix | Delete
else:
[301] Fix | Delete
self._loop = events.get_event_loop()
[302] Fix | Delete
[303] Fix | Delete
if lock is None:
[304] Fix | Delete
lock = Lock(loop=self._loop)
[305] Fix | Delete
elif lock._loop is not self._loop:
[306] Fix | Delete
raise ValueError("loop argument must agree with lock")
[307] Fix | Delete
[308] Fix | Delete
self._lock = lock
[309] Fix | Delete
# Export the lock's locked(), acquire() and release() methods.
[310] Fix | Delete
self.locked = lock.locked
[311] Fix | Delete
self.acquire = lock.acquire
[312] Fix | Delete
self.release = lock.release
[313] Fix | Delete
[314] Fix | Delete
self._waiters = collections.deque()
[315] Fix | Delete
[316] Fix | Delete
def __repr__(self):
[317] Fix | Delete
res = super().__repr__()
[318] Fix | Delete
extra = 'locked' if self.locked() else 'unlocked'
[319] Fix | Delete
if self._waiters:
[320] Fix | Delete
extra = '{},waiters:{}'.format(extra, len(self._waiters))
[321] Fix | Delete
return '<{} [{}]>'.format(res[1:-1], extra)
[322] Fix | Delete
[323] Fix | Delete
@coroutine
[324] Fix | Delete
def wait(self):
[325] Fix | Delete
"""Wait until notified.
[326] Fix | Delete
[327] Fix | Delete
If the calling coroutine has not acquired the lock when this
[328] Fix | Delete
method is called, a RuntimeError is raised.
[329] Fix | Delete
[330] Fix | Delete
This method releases the underlying lock, and then blocks
[331] Fix | Delete
until it is awakened by a notify() or notify_all() call for
[332] Fix | Delete
the same condition variable in another coroutine. Once
[333] Fix | Delete
awakened, it re-acquires the lock and returns True.
[334] Fix | Delete
"""
[335] Fix | Delete
if not self.locked():
[336] Fix | Delete
raise RuntimeError('cannot wait on un-acquired lock')
[337] Fix | Delete
[338] Fix | Delete
self.release()
[339] Fix | Delete
try:
[340] Fix | Delete
fut = self._loop.create_future()
[341] Fix | Delete
self._waiters.append(fut)
[342] Fix | Delete
try:
[343] Fix | Delete
yield from fut
[344] Fix | Delete
return True
[345] Fix | Delete
finally:
[346] Fix | Delete
self._waiters.remove(fut)
[347] Fix | Delete
[348] Fix | Delete
finally:
[349] Fix | Delete
# Must reacquire lock even if wait is cancelled
[350] Fix | Delete
cancelled = False
[351] Fix | Delete
while True:
[352] Fix | Delete
try:
[353] Fix | Delete
yield from self.acquire()
[354] Fix | Delete
break
[355] Fix | Delete
except futures.CancelledError:
[356] Fix | Delete
cancelled = True
[357] Fix | Delete
[358] Fix | Delete
if cancelled:
[359] Fix | Delete
raise futures.CancelledError
[360] Fix | Delete
[361] Fix | Delete
@coroutine
[362] Fix | Delete
def wait_for(self, predicate):
[363] Fix | Delete
"""Wait until a predicate becomes true.
[364] Fix | Delete
[365] Fix | Delete
The predicate should be a callable which result will be
[366] Fix | Delete
interpreted as a boolean value. The final predicate value is
[367] Fix | Delete
the return value.
[368] Fix | Delete
"""
[369] Fix | Delete
result = predicate()
[370] Fix | Delete
while not result:
[371] Fix | Delete
yield from self.wait()
[372] Fix | Delete
result = predicate()
[373] Fix | Delete
return result
[374] Fix | Delete
[375] Fix | Delete
def notify(self, n=1):
[376] Fix | Delete
"""By default, wake up one coroutine waiting on this condition, if any.
[377] Fix | Delete
If the calling coroutine has not acquired the lock when this method
[378] Fix | Delete
is called, a RuntimeError is raised.
[379] Fix | Delete
[380] Fix | Delete
This method wakes up at most n of the coroutines waiting for the
[381] Fix | Delete
condition variable; it is a no-op if no coroutines are waiting.
[382] Fix | Delete
[383] Fix | Delete
Note: an awakened coroutine does not actually return from its
[384] Fix | Delete
wait() call until it can reacquire the lock. Since notify() does
[385] Fix | Delete
not release the lock, its caller should.
[386] Fix | Delete
"""
[387] Fix | Delete
if not self.locked():
[388] Fix | Delete
raise RuntimeError('cannot notify on un-acquired lock')
[389] Fix | Delete
[390] Fix | Delete
idx = 0
[391] Fix | Delete
for fut in self._waiters:
[392] Fix | Delete
if idx >= n:
[393] Fix | Delete
break
[394] Fix | Delete
[395] Fix | Delete
if not fut.done():
[396] Fix | Delete
idx += 1
[397] Fix | Delete
fut.set_result(False)
[398] Fix | Delete
[399] Fix | Delete
def notify_all(self):
[400] Fix | Delete
"""Wake up all threads waiting on this condition. This method acts
[401] Fix | Delete
like notify(), but wakes up all waiting threads instead of one. If the
[402] Fix | Delete
calling thread has not acquired the lock when this method is called,
[403] Fix | Delete
a RuntimeError is raised.
[404] Fix | Delete
"""
[405] Fix | Delete
self.notify(len(self._waiters))
[406] Fix | Delete
[407] Fix | Delete
[408] Fix | Delete
class Semaphore(_ContextManagerMixin):
[409] Fix | Delete
"""A Semaphore implementation.
[410] Fix | Delete
[411] Fix | Delete
A semaphore manages an internal counter which is decremented by each
[412] Fix | Delete
acquire() call and incremented by each release() call. The counter
[413] Fix | Delete
can never go below zero; when acquire() finds that it is zero, it blocks,
[414] Fix | Delete
waiting until some other thread calls release().
[415] Fix | Delete
[416] Fix | Delete
Semaphores also support the context management protocol.
[417] Fix | Delete
[418] Fix | Delete
The optional argument gives the initial value for the internal
[419] Fix | Delete
counter; it defaults to 1. If the value given is less than 0,
[420] Fix | Delete
ValueError is raised.
[421] Fix | Delete
"""
[422] Fix | Delete
[423] Fix | Delete
def __init__(self, value=1, *, loop=None):
[424] Fix | Delete
if value < 0:
[425] Fix | Delete
raise ValueError("Semaphore initial value must be >= 0")
[426] Fix | Delete
self._value = value
[427] Fix | Delete
self._waiters = collections.deque()
[428] Fix | Delete
if loop is not None:
[429] Fix | Delete
self._loop = loop
[430] Fix | Delete
else:
[431] Fix | Delete
self._loop = events.get_event_loop()
[432] Fix | Delete
[433] Fix | Delete
def __repr__(self):
[434] Fix | Delete
res = super().__repr__()
[435] Fix | Delete
extra = 'locked' if self.locked() else 'unlocked,value:{}'.format(
[436] Fix | Delete
self._value)
[437] Fix | Delete
if self._waiters:
[438] Fix | Delete
extra = '{},waiters:{}'.format(extra, len(self._waiters))
[439] Fix | Delete
return '<{} [{}]>'.format(res[1:-1], extra)
[440] Fix | Delete
[441] Fix | Delete
def _wake_up_next(self):
[442] Fix | Delete
while self._waiters:
[443] Fix | Delete
waiter = self._waiters.popleft()
[444] Fix | Delete
if not waiter.done():
[445] Fix | Delete
waiter.set_result(None)
[446] Fix | Delete
return
[447] Fix | Delete
[448] Fix | Delete
def locked(self):
[449] Fix | Delete
"""Returns True if semaphore can not be acquired immediately."""
[450] Fix | Delete
return self._value == 0
[451] Fix | Delete
[452] Fix | Delete
@coroutine
[453] Fix | Delete
def acquire(self):
[454] Fix | Delete
"""Acquire a semaphore.
[455] Fix | Delete
[456] Fix | Delete
If the internal counter is larger than zero on entry,
[457] Fix | Delete
decrement it by one and return True immediately. If it is
[458] Fix | Delete
zero on entry, block, waiting until some other coroutine has
[459] Fix | Delete
called release() to make it larger than 0, and then return
[460] Fix | Delete
True.
[461] Fix | Delete
"""
[462] Fix | Delete
while self._value <= 0:
[463] Fix | Delete
fut = self._loop.create_future()
[464] Fix | Delete
self._waiters.append(fut)
[465] Fix | Delete
try:
[466] Fix | Delete
yield from fut
[467] Fix | Delete
except:
[468] Fix | Delete
# See the similar code in Queue.get.
[469] Fix | Delete
fut.cancel()
[470] Fix | Delete
if self._value > 0 and not fut.cancelled():
[471] Fix | Delete
self._wake_up_next()
[472] Fix | Delete
raise
[473] Fix | Delete
self._value -= 1
[474] Fix | Delete
return True
[475] Fix | Delete
[476] Fix | Delete
def release(self):
[477] Fix | Delete
"""Release a semaphore, incrementing the internal counter by one.
[478] Fix | Delete
When it was zero on entry and another coroutine is waiting for it to
[479] Fix | Delete
become larger than zero again, wake up that coroutine.
[480] Fix | Delete
"""
[481] Fix | Delete
self._value += 1
[482] Fix | Delete
self._wake_up_next()
[483] Fix | Delete
[484] Fix | Delete
[485] Fix | Delete
class BoundedSemaphore(Semaphore):
[486] Fix | Delete
"""A bounded semaphore implementation.
[487] Fix | Delete
[488] Fix | Delete
This raises ValueError in release() if it would increase the value
[489] Fix | Delete
above the initial value.
[490] Fix | Delete
"""
[491] Fix | Delete
[492] Fix | Delete
def __init__(self, value=1, *, loop=None):
[493] Fix | Delete
self._bound_value = value
[494] Fix | Delete
super().__init__(value, loop=loop)
[495] Fix | Delete
[496] Fix | Delete
def release(self):
[497] Fix | Delete
if self._value >= self._bound_value:
[498] Fix | Delete
raise ValueError('BoundedSemaphore released too many times')
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function