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