Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3....
File: threading.py
"""Thread module emulating a subset of Java's threading model."""
[0] Fix | Delete
[1] Fix | Delete
import sys as _sys
[2] Fix | Delete
import _thread
[3] Fix | Delete
[4] Fix | Delete
from time import monotonic as _time, sleep as _sleep
[5] Fix | Delete
from traceback import format_exc as _format_exc
[6] Fix | Delete
from _weakrefset import WeakSet
[7] Fix | Delete
from itertools import islice as _islice, count as _count
[8] Fix | Delete
try:
[9] Fix | Delete
from _collections import deque as _deque
[10] Fix | Delete
except ImportError:
[11] Fix | Delete
from collections import deque as _deque
[12] Fix | Delete
[13] Fix | Delete
# Note regarding PEP 8 compliant names
[14] Fix | Delete
# This threading model was originally inspired by Java, and inherited
[15] Fix | Delete
# the convention of camelCase function and method names from that
[16] Fix | Delete
# language. Those original names are not in any imminent danger of
[17] Fix | Delete
# being deprecated (even for Py3k),so this module provides them as an
[18] Fix | Delete
# alias for the PEP 8 compliant names
[19] Fix | Delete
# Note that using the new PEP 8 compliant names facilitates substitution
[20] Fix | Delete
# with the multiprocessing module, which doesn't provide the old
[21] Fix | Delete
# Java inspired names.
[22] Fix | Delete
[23] Fix | Delete
__all__ = ['get_ident', 'active_count', 'Condition', 'current_thread',
[24] Fix | Delete
'enumerate', 'main_thread', 'TIMEOUT_MAX',
[25] Fix | Delete
'Event', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
[26] Fix | Delete
'Barrier', 'BrokenBarrierError', 'Timer', 'ThreadError',
[27] Fix | Delete
'setprofile', 'settrace', 'local', 'stack_size']
[28] Fix | Delete
[29] Fix | Delete
# Rename some stuff so "from threading import *" is safe
[30] Fix | Delete
_start_new_thread = _thread.start_new_thread
[31] Fix | Delete
_allocate_lock = _thread.allocate_lock
[32] Fix | Delete
_set_sentinel = _thread._set_sentinel
[33] Fix | Delete
get_ident = _thread.get_ident
[34] Fix | Delete
ThreadError = _thread.error
[35] Fix | Delete
try:
[36] Fix | Delete
_CRLock = _thread.RLock
[37] Fix | Delete
except AttributeError:
[38] Fix | Delete
_CRLock = None
[39] Fix | Delete
TIMEOUT_MAX = _thread.TIMEOUT_MAX
[40] Fix | Delete
del _thread
[41] Fix | Delete
[42] Fix | Delete
[43] Fix | Delete
# Support for profile and trace hooks
[44] Fix | Delete
[45] Fix | Delete
_profile_hook = None
[46] Fix | Delete
_trace_hook = None
[47] Fix | Delete
[48] Fix | Delete
def setprofile(func):
[49] Fix | Delete
"""Set a profile function for all threads started from the threading module.
[50] Fix | Delete
[51] Fix | Delete
The func will be passed to sys.setprofile() for each thread, before its
[52] Fix | Delete
run() method is called.
[53] Fix | Delete
[54] Fix | Delete
"""
[55] Fix | Delete
global _profile_hook
[56] Fix | Delete
_profile_hook = func
[57] Fix | Delete
[58] Fix | Delete
def settrace(func):
[59] Fix | Delete
"""Set a trace function for all threads started from the threading module.
[60] Fix | Delete
[61] Fix | Delete
The func will be passed to sys.settrace() for each thread, before its run()
[62] Fix | Delete
method is called.
[63] Fix | Delete
[64] Fix | Delete
"""
[65] Fix | Delete
global _trace_hook
[66] Fix | Delete
_trace_hook = func
[67] Fix | Delete
[68] Fix | Delete
# Synchronization classes
[69] Fix | Delete
[70] Fix | Delete
Lock = _allocate_lock
[71] Fix | Delete
[72] Fix | Delete
def RLock(*args, **kwargs):
[73] Fix | Delete
"""Factory function that returns a new reentrant lock.
[74] Fix | Delete
[75] Fix | Delete
A reentrant lock must be released by the thread that acquired it. Once a
[76] Fix | Delete
thread has acquired a reentrant lock, the same thread may acquire it again
[77] Fix | Delete
without blocking; the thread must release it once for each time it has
[78] Fix | Delete
acquired it.
[79] Fix | Delete
[80] Fix | Delete
"""
[81] Fix | Delete
if _CRLock is None:
[82] Fix | Delete
return _PyRLock(*args, **kwargs)
[83] Fix | Delete
return _CRLock(*args, **kwargs)
[84] Fix | Delete
[85] Fix | Delete
class _RLock:
[86] Fix | Delete
"""This class implements reentrant lock objects.
[87] Fix | Delete
[88] Fix | Delete
A reentrant lock must be released by the thread that acquired it. Once a
[89] Fix | Delete
thread has acquired a reentrant lock, the same thread may acquire it
[90] Fix | Delete
again without blocking; the thread must release it once for each time it
[91] Fix | Delete
has acquired it.
[92] Fix | Delete
[93] Fix | Delete
"""
[94] Fix | Delete
[95] Fix | Delete
def __init__(self):
[96] Fix | Delete
self._block = _allocate_lock()
[97] Fix | Delete
self._owner = None
[98] Fix | Delete
self._count = 0
[99] Fix | Delete
[100] Fix | Delete
def __repr__(self):
[101] Fix | Delete
owner = self._owner
[102] Fix | Delete
try:
[103] Fix | Delete
owner = _active[owner].name
[104] Fix | Delete
except KeyError:
[105] Fix | Delete
pass
[106] Fix | Delete
return "<%s %s.%s object owner=%r count=%d at %s>" % (
[107] Fix | Delete
"locked" if self._block.locked() else "unlocked",
[108] Fix | Delete
self.__class__.__module__,
[109] Fix | Delete
self.__class__.__qualname__,
[110] Fix | Delete
owner,
[111] Fix | Delete
self._count,
[112] Fix | Delete
hex(id(self))
[113] Fix | Delete
)
[114] Fix | Delete
[115] Fix | Delete
def acquire(self, blocking=True, timeout=-1):
[116] Fix | Delete
"""Acquire a lock, blocking or non-blocking.
[117] Fix | Delete
[118] Fix | Delete
When invoked without arguments: if this thread already owns the lock,
[119] Fix | Delete
increment the recursion level by one, and return immediately. Otherwise,
[120] Fix | Delete
if another thread owns the lock, block until the lock is unlocked. Once
[121] Fix | Delete
the lock is unlocked (not owned by any thread), then grab ownership, set
[122] Fix | Delete
the recursion level to one, and return. If more than one thread is
[123] Fix | Delete
blocked waiting until the lock is unlocked, only one at a time will be
[124] Fix | Delete
able to grab ownership of the lock. There is no return value in this
[125] Fix | Delete
case.
[126] Fix | Delete
[127] Fix | Delete
When invoked with the blocking argument set to true, do the same thing
[128] Fix | Delete
as when called without arguments, and return true.
[129] Fix | Delete
[130] Fix | Delete
When invoked with the blocking argument set to false, do not block. If a
[131] Fix | Delete
call without an argument would block, return false immediately;
[132] Fix | Delete
otherwise, do the same thing as when called without arguments, and
[133] Fix | Delete
return true.
[134] Fix | Delete
[135] Fix | Delete
When invoked with the floating-point timeout argument set to a positive
[136] Fix | Delete
value, block for at most the number of seconds specified by timeout
[137] Fix | Delete
and as long as the lock cannot be acquired. Return true if the lock has
[138] Fix | Delete
been acquired, false if the timeout has elapsed.
[139] Fix | Delete
[140] Fix | Delete
"""
[141] Fix | Delete
me = get_ident()
[142] Fix | Delete
if self._owner == me:
[143] Fix | Delete
self._count += 1
[144] Fix | Delete
return 1
[145] Fix | Delete
rc = self._block.acquire(blocking, timeout)
[146] Fix | Delete
if rc:
[147] Fix | Delete
self._owner = me
[148] Fix | Delete
self._count = 1
[149] Fix | Delete
return rc
[150] Fix | Delete
[151] Fix | Delete
__enter__ = acquire
[152] Fix | Delete
[153] Fix | Delete
def release(self):
[154] Fix | Delete
"""Release a lock, decrementing the recursion level.
[155] Fix | Delete
[156] Fix | Delete
If after the decrement it is zero, reset the lock to unlocked (not owned
[157] Fix | Delete
by any thread), and if any other threads are blocked waiting for the
[158] Fix | Delete
lock to become unlocked, allow exactly one of them to proceed. If after
[159] Fix | Delete
the decrement the recursion level is still nonzero, the lock remains
[160] Fix | Delete
locked and owned by the calling thread.
[161] Fix | Delete
[162] Fix | Delete
Only call this method when the calling thread owns the lock. A
[163] Fix | Delete
RuntimeError is raised if this method is called when the lock is
[164] Fix | Delete
unlocked.
[165] Fix | Delete
[166] Fix | Delete
There is no return value.
[167] Fix | Delete
[168] Fix | Delete
"""
[169] Fix | Delete
if self._owner != get_ident():
[170] Fix | Delete
raise RuntimeError("cannot release un-acquired lock")
[171] Fix | Delete
self._count = count = self._count - 1
[172] Fix | Delete
if not count:
[173] Fix | Delete
self._owner = None
[174] Fix | Delete
self._block.release()
[175] Fix | Delete
[176] Fix | Delete
def __exit__(self, t, v, tb):
[177] Fix | Delete
self.release()
[178] Fix | Delete
[179] Fix | Delete
# Internal methods used by condition variables
[180] Fix | Delete
[181] Fix | Delete
def _acquire_restore(self, state):
[182] Fix | Delete
self._block.acquire()
[183] Fix | Delete
self._count, self._owner = state
[184] Fix | Delete
[185] Fix | Delete
def _release_save(self):
[186] Fix | Delete
if self._count == 0:
[187] Fix | Delete
raise RuntimeError("cannot release un-acquired lock")
[188] Fix | Delete
count = self._count
[189] Fix | Delete
self._count = 0
[190] Fix | Delete
owner = self._owner
[191] Fix | Delete
self._owner = None
[192] Fix | Delete
self._block.release()
[193] Fix | Delete
return (count, owner)
[194] Fix | Delete
[195] Fix | Delete
def _is_owned(self):
[196] Fix | Delete
return self._owner == get_ident()
[197] Fix | Delete
[198] Fix | Delete
_PyRLock = _RLock
[199] Fix | Delete
[200] Fix | Delete
[201] Fix | Delete
class Condition:
[202] Fix | Delete
"""Class that implements a condition variable.
[203] Fix | Delete
[204] Fix | Delete
A condition variable allows one or more threads to wait until they are
[205] Fix | Delete
notified by another thread.
[206] Fix | Delete
[207] Fix | Delete
If the lock argument is given and not None, it must be a Lock or RLock
[208] Fix | Delete
object, and it is used as the underlying lock. Otherwise, a new RLock object
[209] Fix | Delete
is created and used as the underlying lock.
[210] Fix | Delete
[211] Fix | Delete
"""
[212] Fix | Delete
[213] Fix | Delete
def __init__(self, lock=None):
[214] Fix | Delete
if lock is None:
[215] Fix | Delete
lock = RLock()
[216] Fix | Delete
self._lock = lock
[217] Fix | Delete
# Export the lock's acquire() and release() methods
[218] Fix | Delete
self.acquire = lock.acquire
[219] Fix | Delete
self.release = lock.release
[220] Fix | Delete
# If the lock defines _release_save() and/or _acquire_restore(),
[221] Fix | Delete
# these override the default implementations (which just call
[222] Fix | Delete
# release() and acquire() on the lock). Ditto for _is_owned().
[223] Fix | Delete
try:
[224] Fix | Delete
self._release_save = lock._release_save
[225] Fix | Delete
except AttributeError:
[226] Fix | Delete
pass
[227] Fix | Delete
try:
[228] Fix | Delete
self._acquire_restore = lock._acquire_restore
[229] Fix | Delete
except AttributeError:
[230] Fix | Delete
pass
[231] Fix | Delete
try:
[232] Fix | Delete
self._is_owned = lock._is_owned
[233] Fix | Delete
except AttributeError:
[234] Fix | Delete
pass
[235] Fix | Delete
self._waiters = _deque()
[236] Fix | Delete
[237] Fix | Delete
def __enter__(self):
[238] Fix | Delete
return self._lock.__enter__()
[239] Fix | Delete
[240] Fix | Delete
def __exit__(self, *args):
[241] Fix | Delete
return self._lock.__exit__(*args)
[242] Fix | Delete
[243] Fix | Delete
def __repr__(self):
[244] Fix | Delete
return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
[245] Fix | Delete
[246] Fix | Delete
def _release_save(self):
[247] Fix | Delete
self._lock.release() # No state to save
[248] Fix | Delete
[249] Fix | Delete
def _acquire_restore(self, x):
[250] Fix | Delete
self._lock.acquire() # Ignore saved state
[251] Fix | Delete
[252] Fix | Delete
def _is_owned(self):
[253] Fix | Delete
# Return True if lock is owned by current_thread.
[254] Fix | Delete
# This method is called only if _lock doesn't have _is_owned().
[255] Fix | Delete
if self._lock.acquire(0):
[256] Fix | Delete
self._lock.release()
[257] Fix | Delete
return False
[258] Fix | Delete
else:
[259] Fix | Delete
return True
[260] Fix | Delete
[261] Fix | Delete
def wait(self, timeout=None):
[262] Fix | Delete
"""Wait until notified or until a timeout occurs.
[263] Fix | Delete
[264] Fix | Delete
If the calling thread has not acquired the lock when this method is
[265] Fix | Delete
called, a RuntimeError is raised.
[266] Fix | Delete
[267] Fix | Delete
This method releases the underlying lock, and then blocks until it is
[268] Fix | Delete
awakened by a notify() or notify_all() call for the same condition
[269] Fix | Delete
variable in another thread, or until the optional timeout occurs. Once
[270] Fix | Delete
awakened or timed out, it re-acquires the lock and returns.
[271] Fix | Delete
[272] Fix | Delete
When the timeout argument is present and not None, it should be a
[273] Fix | Delete
floating point number specifying a timeout for the operation in seconds
[274] Fix | Delete
(or fractions thereof).
[275] Fix | Delete
[276] Fix | Delete
When the underlying lock is an RLock, it is not released using its
[277] Fix | Delete
release() method, since this may not actually unlock the lock when it
[278] Fix | Delete
was acquired multiple times recursively. Instead, an internal interface
[279] Fix | Delete
of the RLock class is used, which really unlocks it even when it has
[280] Fix | Delete
been recursively acquired several times. Another internal interface is
[281] Fix | Delete
then used to restore the recursion level when the lock is reacquired.
[282] Fix | Delete
[283] Fix | Delete
"""
[284] Fix | Delete
if not self._is_owned():
[285] Fix | Delete
raise RuntimeError("cannot wait on un-acquired lock")
[286] Fix | Delete
waiter = _allocate_lock()
[287] Fix | Delete
waiter.acquire()
[288] Fix | Delete
self._waiters.append(waiter)
[289] Fix | Delete
saved_state = self._release_save()
[290] Fix | Delete
gotit = False
[291] Fix | Delete
try: # restore state no matter what (e.g., KeyboardInterrupt)
[292] Fix | Delete
if timeout is None:
[293] Fix | Delete
waiter.acquire()
[294] Fix | Delete
gotit = True
[295] Fix | Delete
else:
[296] Fix | Delete
if timeout > 0:
[297] Fix | Delete
# rhbz#2003758: Avoid waiter.acquire(True, timeout) since
[298] Fix | Delete
# it uses the system clock internally.
[299] Fix | Delete
#
[300] Fix | Delete
# Balancing act: We can't afford a pure busy loop, so we
[301] Fix | Delete
# have to sleep; but if we sleep the whole timeout time,
[302] Fix | Delete
# we'll be unresponsive. The scheme here sleeps very
[303] Fix | Delete
# little at first, longer as time goes on, but never longer
[304] Fix | Delete
# than 20 times per second (or the timeout time remaining).
[305] Fix | Delete
endtime = _time() + timeout
[306] Fix | Delete
delay = 0.0005 # 500 us -> initial delay of 1 ms
[307] Fix | Delete
while True:
[308] Fix | Delete
gotit = waiter.acquire(0)
[309] Fix | Delete
if gotit:
[310] Fix | Delete
break
[311] Fix | Delete
remaining = min(endtime - _time(), timeout)
[312] Fix | Delete
if remaining <= 0:
[313] Fix | Delete
break
[314] Fix | Delete
delay = min(delay * 2, remaining, .05)
[315] Fix | Delete
_sleep(delay)
[316] Fix | Delete
else:
[317] Fix | Delete
gotit = waiter.acquire(False)
[318] Fix | Delete
return gotit
[319] Fix | Delete
finally:
[320] Fix | Delete
self._acquire_restore(saved_state)
[321] Fix | Delete
if not gotit:
[322] Fix | Delete
try:
[323] Fix | Delete
self._waiters.remove(waiter)
[324] Fix | Delete
except ValueError:
[325] Fix | Delete
pass
[326] Fix | Delete
[327] Fix | Delete
def wait_for(self, predicate, timeout=None):
[328] Fix | Delete
"""Wait until a condition evaluates to True.
[329] Fix | Delete
[330] Fix | Delete
predicate should be a callable which result will be interpreted as a
[331] Fix | Delete
boolean value. A timeout may be provided giving the maximum time to
[332] Fix | Delete
wait.
[333] Fix | Delete
[334] Fix | Delete
"""
[335] Fix | Delete
endtime = None
[336] Fix | Delete
waittime = timeout
[337] Fix | Delete
result = predicate()
[338] Fix | Delete
while not result:
[339] Fix | Delete
if waittime is not None:
[340] Fix | Delete
if endtime is None:
[341] Fix | Delete
endtime = _time() + waittime
[342] Fix | Delete
else:
[343] Fix | Delete
waittime = endtime - _time()
[344] Fix | Delete
if waittime <= 0:
[345] Fix | Delete
break
[346] Fix | Delete
self.wait(waittime)
[347] Fix | Delete
result = predicate()
[348] Fix | Delete
return result
[349] Fix | Delete
[350] Fix | Delete
def notify(self, n=1):
[351] Fix | Delete
"""Wake up one or more threads waiting on this condition, if any.
[352] Fix | Delete
[353] Fix | Delete
If the calling thread has not acquired the lock when this method is
[354] Fix | Delete
called, a RuntimeError is raised.
[355] Fix | Delete
[356] Fix | Delete
This method wakes up at most n of the threads waiting for the condition
[357] Fix | Delete
variable; it is a no-op if no threads are waiting.
[358] Fix | Delete
[359] Fix | Delete
"""
[360] Fix | Delete
if not self._is_owned():
[361] Fix | Delete
raise RuntimeError("cannot notify on un-acquired lock")
[362] Fix | Delete
all_waiters = self._waiters
[363] Fix | Delete
waiters_to_notify = _deque(_islice(all_waiters, n))
[364] Fix | Delete
if not waiters_to_notify:
[365] Fix | Delete
return
[366] Fix | Delete
for waiter in waiters_to_notify:
[367] Fix | Delete
waiter.release()
[368] Fix | Delete
try:
[369] Fix | Delete
all_waiters.remove(waiter)
[370] Fix | Delete
except ValueError:
[371] Fix | Delete
pass
[372] Fix | Delete
[373] Fix | Delete
def notify_all(self):
[374] Fix | Delete
"""Wake up all threads waiting on this condition.
[375] Fix | Delete
[376] Fix | Delete
If the calling thread has not acquired the lock when this method
[377] Fix | Delete
is called, a RuntimeError is raised.
[378] Fix | Delete
[379] Fix | Delete
"""
[380] Fix | Delete
self.notify(len(self._waiters))
[381] Fix | Delete
[382] Fix | Delete
notifyAll = notify_all
[383] Fix | Delete
[384] Fix | Delete
[385] Fix | Delete
class Semaphore:
[386] Fix | Delete
"""This class implements semaphore objects.
[387] Fix | Delete
[388] Fix | Delete
Semaphores manage a counter representing the number of release() calls minus
[389] Fix | Delete
the number of acquire() calls, plus an initial value. The acquire() method
[390] Fix | Delete
blocks if necessary until it can return without making the counter
[391] Fix | Delete
negative. If not given, value defaults to 1.
[392] Fix | Delete
[393] Fix | Delete
"""
[394] Fix | Delete
[395] Fix | Delete
# After Tim Peters' semaphore class, but not quite the same (no maximum)
[396] Fix | Delete
[397] Fix | Delete
def __init__(self, value=1):
[398] Fix | Delete
if value < 0:
[399] Fix | Delete
raise ValueError("semaphore initial value must be >= 0")
[400] Fix | Delete
self._cond = Condition(Lock())
[401] Fix | Delete
self._value = value
[402] Fix | Delete
[403] Fix | Delete
def acquire(self, blocking=True, timeout=None):
[404] Fix | Delete
"""Acquire a semaphore, decrementing the internal counter by one.
[405] Fix | Delete
[406] Fix | Delete
When invoked without arguments: if the internal counter is larger than
[407] Fix | Delete
zero on entry, decrement it by one and return immediately. If it is zero
[408] Fix | Delete
on entry, block, waiting until some other thread has called release() to
[409] Fix | Delete
make it larger than zero. This is done with proper interlocking so that
[410] Fix | Delete
if multiple acquire() calls are blocked, release() will wake exactly one
[411] Fix | Delete
of them up. The implementation may pick one at random, so the order in
[412] Fix | Delete
which blocked threads are awakened should not be relied on. There is no
[413] Fix | Delete
return value in this case.
[414] Fix | Delete
[415] Fix | Delete
When invoked with blocking set to true, do the same thing as when called
[416] Fix | Delete
without arguments, and return true.
[417] Fix | Delete
[418] Fix | Delete
When invoked with blocking set to false, do not block. If a call without
[419] Fix | Delete
an argument would block, return false immediately; otherwise, do the
[420] Fix | Delete
same thing as when called without arguments, and return true.
[421] Fix | Delete
[422] Fix | Delete
When invoked with a timeout other than None, it will block for at
[423] Fix | Delete
most timeout seconds. If acquire does not complete successfully in
[424] Fix | Delete
that interval, return false. Return true otherwise.
[425] Fix | Delete
[426] Fix | Delete
"""
[427] Fix | Delete
if not blocking and timeout is not None:
[428] Fix | Delete
raise ValueError("can't specify timeout for non-blocking acquire")
[429] Fix | Delete
rc = False
[430] Fix | Delete
endtime = None
[431] Fix | Delete
with self._cond:
[432] Fix | Delete
while self._value == 0:
[433] Fix | Delete
if not blocking:
[434] Fix | Delete
break
[435] Fix | Delete
if timeout is not None:
[436] Fix | Delete
if endtime is None:
[437] Fix | Delete
endtime = _time() + timeout
[438] Fix | Delete
else:
[439] Fix | Delete
timeout = endtime - _time()
[440] Fix | Delete
if timeout <= 0:
[441] Fix | Delete
break
[442] Fix | Delete
self._cond.wait(timeout)
[443] Fix | Delete
else:
[444] Fix | Delete
self._value -= 1
[445] Fix | Delete
rc = True
[446] Fix | Delete
return rc
[447] Fix | Delete
[448] Fix | Delete
__enter__ = acquire
[449] Fix | Delete
[450] Fix | Delete
def release(self):
[451] Fix | Delete
"""Release a semaphore, incrementing the internal counter by one.
[452] Fix | Delete
[453] Fix | Delete
When the counter is zero on entry and another thread is waiting for it
[454] Fix | Delete
to become larger than zero again, wake up that thread.
[455] Fix | Delete
[456] Fix | Delete
"""
[457] Fix | Delete
with self._cond:
[458] Fix | Delete
self._value += 1
[459] Fix | Delete
self._cond.notify()
[460] Fix | Delete
[461] Fix | Delete
def __exit__(self, t, v, tb):
[462] Fix | Delete
self.release()
[463] Fix | Delete
[464] Fix | Delete
[465] Fix | Delete
class BoundedSemaphore(Semaphore):
[466] Fix | Delete
"""Implements a bounded semaphore.
[467] Fix | Delete
[468] Fix | Delete
A bounded semaphore checks to make sure its current value doesn't exceed its
[469] Fix | Delete
initial value. If it does, ValueError is raised. In most situations
[470] Fix | Delete
semaphores are used to guard resources with limited capacity.
[471] Fix | Delete
[472] Fix | Delete
If the semaphore is released too many times it's a sign of a bug. If not
[473] Fix | Delete
given, value defaults to 1.
[474] Fix | Delete
[475] Fix | Delete
Like regular semaphores, bounded semaphores manage a counter representing
[476] Fix | Delete
the number of release() calls minus the number of acquire() calls, plus an
[477] Fix | Delete
initial value. The acquire() method blocks if necessary until it can return
[478] Fix | Delete
without making the counter negative. If not given, value defaults to 1.
[479] Fix | Delete
[480] Fix | Delete
"""
[481] Fix | Delete
[482] Fix | Delete
def __init__(self, value=1):
[483] Fix | Delete
Semaphore.__init__(self, value)
[484] Fix | Delete
self._initial_value = value
[485] Fix | Delete
[486] Fix | Delete
def release(self):
[487] Fix | Delete
"""Release a semaphore, incrementing the internal counter by one.
[488] Fix | Delete
[489] Fix | Delete
When the counter is zero on entry and another thread is waiting for it
[490] Fix | Delete
to become larger than zero again, wake up that thread.
[491] Fix | Delete
[492] Fix | Delete
If the number of releases exceeds the number of acquires,
[493] Fix | Delete
raise a ValueError.
[494] Fix | Delete
[495] Fix | Delete
"""
[496] Fix | Delete
with self._cond:
[497] Fix | Delete
if self._value >= self._initial_value:
[498] Fix | Delete
raise ValueError("Semaphore released too many times")
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function