| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | """Synchronization primitives.""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-10 18:36:12 -05:00
										 |  |  | __all__ = ('Lock', 'Event', 'Condition', 'Semaphore', 'BoundedSemaphore') | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | import collections | 
					
						
							| 
									
										
										
										
											2017-12-09 20:00:05 +02:00
										 |  |  | import warnings | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | from . import events | 
					
						
							| 
									
										
										
										
											2018-09-11 10:13:04 -07:00
										 |  |  | from . import exceptions | 
					
						
							| 
									
										
										
										
											2014-01-25 16:51:57 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-13 15:15:56 -04:00
										 |  |  | class _ContextManagerMixin: | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |     async def __aenter__(self): | 
					
						
							|  |  |  |         await self.acquire() | 
					
						
							| 
									
										
										
										
											2017-11-28 14:43:52 +01:00
										 |  |  |         # We have no use for the "as ..."  clause in the with | 
					
						
							|  |  |  |         # statement for locks. | 
					
						
							|  |  |  |         return None | 
					
						
							| 
									
										
										
										
											2015-05-13 15:15:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |     async def __aexit__(self, exc_type, exc, tb): | 
					
						
							| 
									
										
										
										
											2017-11-28 14:43:52 +01:00
										 |  |  |         self.release() | 
					
						
							| 
									
										
										
										
											2015-05-13 15:15:56 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Lock(_ContextManagerMixin): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     """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. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-11 17:35:49 +02:00
										 |  |  |     acquire() is a coroutine and should be called with 'await'. | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-11 17:35:49 +02:00
										 |  |  |     Locks also support the asynchronous context management protocol. | 
					
						
							|  |  |  |     'async with lock' statement should be used. | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Usage: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         lock = Lock() | 
					
						
							|  |  |  |         ... | 
					
						
							| 
									
										
										
										
											2017-12-11 17:35:49 +02:00
										 |  |  |         await lock.acquire() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         try: | 
					
						
							|  |  |  |             ... | 
					
						
							|  |  |  |         finally: | 
					
						
							|  |  |  |             lock.release() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Context manager usage: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         lock = Lock() | 
					
						
							|  |  |  |         ... | 
					
						
							| 
									
										
										
										
											2017-12-11 17:35:49 +02:00
										 |  |  |         async with lock: | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |              ... | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Lock objects can be tested for locking state: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not lock.locked(): | 
					
						
							| 
									
										
										
										
											2017-12-11 17:35:49 +02:00
										 |  |  |            await lock.acquire() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         else: | 
					
						
							|  |  |  |            # lock is acquired | 
					
						
							|  |  |  |            ... | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __init__(self, *, loop=None): | 
					
						
							| 
									
										
										
										
											2019-06-05 03:33:27 -06:00
										 |  |  |         self._waiters = None | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self._locked = False | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         if loop is None: | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             self._loop = events.get_event_loop() | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         else: | 
					
						
							|  |  |  |             self._loop = loop | 
					
						
							|  |  |  |             warnings.warn("The loop argument is deprecated since Python 3.8, " | 
					
						
							|  |  |  |                           "and scheduled for removal in Python 3.10.", | 
					
						
							|  |  |  |                           DeprecationWarning, stacklevel=2) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __repr__(self): | 
					
						
							|  |  |  |         res = super().__repr__() | 
					
						
							|  |  |  |         extra = 'locked' if self._locked else 'unlocked' | 
					
						
							|  |  |  |         if self._waiters: | 
					
						
							| 
									
										
										
										
											2017-12-10 18:36:12 -05:00
										 |  |  |             extra = f'{extra}, waiters:{len(self._waiters)}' | 
					
						
							|  |  |  |         return f'<{res[1:-1]} [{extra}]>' | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def locked(self): | 
					
						
							| 
									
										
										
										
											2013-12-02 14:31:16 +01:00
										 |  |  |         """Return True if lock is acquired.""" | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         return self._locked | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |     async def acquire(self): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         """Acquire a lock.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         This method blocks until the lock is unlocked, then sets it to | 
					
						
							|  |  |  |         locked and returns True. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2019-06-05 03:33:27 -06:00
										 |  |  |         if (not self._locked and (self._waiters is None or | 
					
						
							|  |  |  |                 all(w.cancelled() for w in self._waiters))): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             self._locked = True | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-05 03:33:27 -06:00
										 |  |  |         if self._waiters is None: | 
					
						
							|  |  |  |             self._waiters = collections.deque() | 
					
						
							| 
									
										
										
										
											2016-05-16 15:38:39 -04:00
										 |  |  |         fut = self._loop.create_future() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self._waiters.append(fut) | 
					
						
							| 
									
										
										
										
											2018-02-03 00:04:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Finally block should be called before the CancelledError | 
					
						
							|  |  |  |         # handling as we don't want CancelledError to call | 
					
						
							|  |  |  |         # _wake_up_first() and attempt to wake up itself. | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2018-02-03 00:04:00 +02:00
										 |  |  |             try: | 
					
						
							|  |  |  |                 await fut | 
					
						
							|  |  |  |             finally: | 
					
						
							|  |  |  |                 self._waiters.remove(fut) | 
					
						
							| 
									
										
										
										
											2018-09-11 10:13:04 -07:00
										 |  |  |         except exceptions.CancelledError: | 
					
						
							| 
									
										
										
										
											2017-06-09 22:17:40 +02:00
										 |  |  |             if not self._locked: | 
					
						
							|  |  |  |                 self._wake_up_first() | 
					
						
							|  |  |  |             raise | 
					
						
							| 
									
										
										
										
											2018-02-03 00:04:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self._locked = True | 
					
						
							|  |  |  |         return True | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def release(self): | 
					
						
							|  |  |  |         """Release 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. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         if self._locked: | 
					
						
							|  |  |  |             self._locked = False | 
					
						
							| 
									
										
										
										
											2017-06-09 22:17:40 +02:00
										 |  |  |             self._wake_up_first() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         else: | 
					
						
							|  |  |  |             raise RuntimeError('Lock is not acquired.') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-09 22:17:40 +02:00
										 |  |  |     def _wake_up_first(self): | 
					
						
							| 
									
										
										
										
											2018-02-03 00:04:00 +02:00
										 |  |  |         """Wake up the first waiter if it isn't done.""" | 
					
						
							| 
									
										
										
										
											2019-06-05 03:33:27 -06:00
										 |  |  |         if not self._waiters: | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2018-02-03 00:04:00 +02:00
										 |  |  |         try: | 
					
						
							|  |  |  |             fut = next(iter(self._waiters)) | 
					
						
							|  |  |  |         except StopIteration: | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # .done() necessarily means that a waiter will wake up later on and | 
					
						
							|  |  |  |         # either take the lock, or, if it was cancelled and lock wasn't | 
					
						
							|  |  |  |         # taken already, will hit this again and wake up a new waiter. | 
					
						
							|  |  |  |         if not fut.done(): | 
					
						
							|  |  |  |             fut.set_result(True) | 
					
						
							| 
									
										
										
										
											2017-06-09 22:17:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Event: | 
					
						
							| 
									
										
										
										
											2013-12-19 12:47:38 -08:00
										 |  |  |     """Asynchronous equivalent to threading.Event.
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __init__(self, *, loop=None): | 
					
						
							|  |  |  |         self._waiters = collections.deque() | 
					
						
							|  |  |  |         self._value = False | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         if loop is None: | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             self._loop = events.get_event_loop() | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         else: | 
					
						
							|  |  |  |             self._loop = loop | 
					
						
							|  |  |  |             warnings.warn("The loop argument is deprecated since Python 3.8, " | 
					
						
							|  |  |  |                           "and scheduled for removal in Python 3.10.", | 
					
						
							|  |  |  |                           DeprecationWarning, stacklevel=2) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __repr__(self): | 
					
						
							|  |  |  |         res = super().__repr__() | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |         extra = 'set' if self._value else 'unset' | 
					
						
							|  |  |  |         if self._waiters: | 
					
						
							| 
									
										
										
										
											2017-12-10 18:36:12 -05:00
										 |  |  |             extra = f'{extra}, waiters:{len(self._waiters)}' | 
					
						
							|  |  |  |         return f'<{res[1:-1]} [{extra}]>' | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def is_set(self): | 
					
						
							| 
									
										
										
										
											2013-12-02 14:31:16 +01:00
										 |  |  |         """Return True if and only if the internal flag is true.""" | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         return self._value | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def set(self): | 
					
						
							|  |  |  |         """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. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         if not self._value: | 
					
						
							|  |  |  |             self._value = True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             for fut in self._waiters: | 
					
						
							|  |  |  |                 if not fut.done(): | 
					
						
							|  |  |  |                     fut.set_result(True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def clear(self): | 
					
						
							|  |  |  |         """Reset the internal flag to false. Subsequently, coroutines calling
 | 
					
						
							|  |  |  |         wait() will block until set() is called to set the internal flag | 
					
						
							|  |  |  |         to true again."""
 | 
					
						
							|  |  |  |         self._value = False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |     async def wait(self): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         """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. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         if self._value: | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-16 15:38:39 -04:00
										 |  |  |         fut = self._loop.create_future() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self._waiters.append(fut) | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |             await fut | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             return True | 
					
						
							|  |  |  |         finally: | 
					
						
							|  |  |  |             self._waiters.remove(fut) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-13 15:15:56 -04:00
										 |  |  | class Condition(_ContextManagerMixin): | 
					
						
							| 
									
										
										
										
											2013-12-19 12:47:38 -08:00
										 |  |  |     """Asynchronous equivalent to threading.Condition.
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     This class implements condition variable objects. A condition variable | 
					
						
							|  |  |  |     allows one or more coroutines to wait until they are notified by another | 
					
						
							|  |  |  |     coroutine. | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     A new Lock object is created and used as the underlying lock. | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-26 17:54:34 +03:00
										 |  |  |     def __init__(self, lock=None, *, loop=None): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         if loop is None: | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |             self._loop = events.get_event_loop() | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         else: | 
					
						
							|  |  |  |             self._loop = loop | 
					
						
							|  |  |  |             warnings.warn("The loop argument is deprecated since Python 3.8, " | 
					
						
							|  |  |  |                           "and scheduled for removal in Python 3.10.", | 
					
						
							|  |  |  |                           DeprecationWarning, stacklevel=2) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-26 17:54:34 +03:00
										 |  |  |         if lock is None: | 
					
						
							| 
									
										
										
										
											2019-09-11 11:20:24 +03:00
										 |  |  |             lock = Lock(loop=loop) | 
					
						
							| 
									
										
										
										
											2014-07-26 17:54:34 +03:00
										 |  |  |         elif lock._loop is not self._loop: | 
					
						
							|  |  |  |             raise ValueError("loop argument must agree with lock") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |         self._lock = lock | 
					
						
							|  |  |  |         # Export the lock's locked(), acquire() and release() methods. | 
					
						
							|  |  |  |         self.locked = lock.locked | 
					
						
							|  |  |  |         self.acquire = lock.acquire | 
					
						
							|  |  |  |         self.release = lock.release | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self._waiters = collections.deque() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __repr__(self): | 
					
						
							|  |  |  |         res = super().__repr__() | 
					
						
							|  |  |  |         extra = 'locked' if self.locked() else 'unlocked' | 
					
						
							|  |  |  |         if self._waiters: | 
					
						
							| 
									
										
										
										
											2017-12-10 18:36:12 -05:00
										 |  |  |             extra = f'{extra}, waiters:{len(self._waiters)}' | 
					
						
							|  |  |  |         return f'<{res[1:-1]} [{extra}]>' | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |     async def wait(self): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         """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. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |         if not self.locked(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             raise RuntimeError('cannot wait on un-acquired lock') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.release() | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2016-05-16 15:38:39 -04:00
										 |  |  |             fut = self._loop.create_future() | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |             self._waiters.append(fut) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             try: | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |                 await fut | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |                 return True | 
					
						
							|  |  |  |             finally: | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |                 self._waiters.remove(fut) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         finally: | 
					
						
							| 
									
										
										
										
											2016-06-11 12:00:07 -04:00
										 |  |  |             # Must reacquire lock even if wait is cancelled | 
					
						
							| 
									
										
										
										
											2018-02-14 11:18:11 +02:00
										 |  |  |             cancelled = False | 
					
						
							| 
									
										
										
										
											2016-06-11 12:00:07 -04:00
										 |  |  |             while True: | 
					
						
							|  |  |  |                 try: | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |                     await self.acquire() | 
					
						
							| 
									
										
										
										
											2016-06-11 12:00:07 -04:00
										 |  |  |                     break | 
					
						
							| 
									
										
										
										
											2018-09-11 10:13:04 -07:00
										 |  |  |                 except exceptions.CancelledError: | 
					
						
							| 
									
										
										
										
											2018-02-14 11:18:11 +02:00
										 |  |  |                     cancelled = True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if cancelled: | 
					
						
							| 
									
										
										
										
											2018-09-11 10:13:04 -07:00
										 |  |  |                 raise exceptions.CancelledError | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |     async def wait_for(self, predicate): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         """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. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         result = predicate() | 
					
						
							|  |  |  |         while not result: | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |             await self.wait() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             result = predicate() | 
					
						
							|  |  |  |         return result | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def notify(self, n=1): | 
					
						
							|  |  |  |         """By 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. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |         if not self.locked(): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             raise RuntimeError('cannot notify on un-acquired lock') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         idx = 0 | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |         for fut in self._waiters: | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             if idx >= n: | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if not fut.done(): | 
					
						
							|  |  |  |                 idx += 1 | 
					
						
							|  |  |  |                 fut.set_result(False) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def notify_all(self): | 
					
						
							|  |  |  |         """Wake 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. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |         self.notify(len(self._waiters)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-13 15:15:56 -04:00
										 |  |  | class Semaphore(_ContextManagerMixin): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     """A 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(). | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-10 23:43:41 +03:00
										 |  |  |     Semaphores also support the context management protocol. | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 15:09:16 -08:00
										 |  |  |     The optional argument gives the initial value for the internal | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     counter; it defaults to 1. If the value given is less than 0, | 
					
						
							|  |  |  |     ValueError is raised. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 15:09:16 -08:00
										 |  |  |     def __init__(self, value=1, *, loop=None): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         if value < 0: | 
					
						
							| 
									
										
										
										
											2013-11-21 11:07:45 -08:00
										 |  |  |             raise ValueError("Semaphore initial value must be >= 0") | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         self._value = value | 
					
						
							|  |  |  |         self._waiters = collections.deque() | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         if loop is None: | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |             self._loop = events.get_event_loop() | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         else: | 
					
						
							|  |  |  |             self._loop = loop | 
					
						
							|  |  |  |             warnings.warn("The loop argument is deprecated since Python 3.8, " | 
					
						
							|  |  |  |                           "and scheduled for removal in Python 3.10.", | 
					
						
							|  |  |  |                           DeprecationWarning, stacklevel=2) | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def __repr__(self): | 
					
						
							|  |  |  |         res = super().__repr__() | 
					
						
							| 
									
										
										
										
											2017-12-10 18:36:12 -05:00
										 |  |  |         extra = 'locked' if self.locked() else f'unlocked, value:{self._value}' | 
					
						
							| 
									
										
										
										
											2013-11-04 13:18:19 -08:00
										 |  |  |         if self._waiters: | 
					
						
							| 
									
										
										
										
											2017-12-10 18:36:12 -05:00
										 |  |  |             extra = f'{extra}, waiters:{len(self._waiters)}' | 
					
						
							|  |  |  |         return f'<{res[1:-1]} [{extra}]>' | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 11:54:45 -07:00
										 |  |  |     def _wake_up_next(self): | 
					
						
							|  |  |  |         while self._waiters: | 
					
						
							|  |  |  |             waiter = self._waiters.popleft() | 
					
						
							|  |  |  |             if not waiter.done(): | 
					
						
							|  |  |  |                 waiter.set_result(None) | 
					
						
							|  |  |  |                 return | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |     def locked(self): | 
					
						
							|  |  |  |         """Returns True if semaphore can not be acquired immediately.""" | 
					
						
							| 
									
										
										
										
											2014-01-25 16:51:57 -08:00
										 |  |  |         return self._value == 0 | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |     async def acquire(self): | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  |         """Acquire 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. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2015-09-29 11:54:45 -07:00
										 |  |  |         while self._value <= 0: | 
					
						
							| 
									
										
										
										
											2016-05-16 15:38:39 -04:00
										 |  |  |             fut = self._loop.create_future() | 
					
						
							| 
									
										
										
										
											2015-09-29 11:54:45 -07:00
										 |  |  |             self._waiters.append(fut) | 
					
						
							|  |  |  |             try: | 
					
						
							| 
									
										
										
										
											2017-12-09 00:23:48 +02:00
										 |  |  |                 await fut | 
					
						
							| 
									
										
										
										
											2015-09-29 11:54:45 -07:00
										 |  |  |             except: | 
					
						
							|  |  |  |                 # See the similar code in Queue.get. | 
					
						
							|  |  |  |                 fut.cancel() | 
					
						
							|  |  |  |                 if self._value > 0 and not fut.cancelled(): | 
					
						
							|  |  |  |                     self._wake_up_next() | 
					
						
							|  |  |  |                 raise | 
					
						
							|  |  |  |         self._value -= 1 | 
					
						
							|  |  |  |         return True | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def release(self): | 
					
						
							|  |  |  |         """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. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         self._value += 1 | 
					
						
							| 
									
										
										
										
											2015-09-29 11:54:45 -07:00
										 |  |  |         self._wake_up_next() | 
					
						
							| 
									
										
										
										
											2013-10-17 13:40:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 15:09:16 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class BoundedSemaphore(Semaphore): | 
					
						
							|  |  |  |     """A bounded semaphore implementation.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     This raises ValueError in release() if it would increase the value | 
					
						
							|  |  |  |     above the initial value. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __init__(self, value=1, *, loop=None): | 
					
						
							| 
									
										
										
										
											2019-09-10 07:55:07 -03:00
										 |  |  |         if loop: | 
					
						
							|  |  |  |             warnings.warn("The loop argument is deprecated since Python 3.8, " | 
					
						
							|  |  |  |                           "and scheduled for removal in Python 3.10.", | 
					
						
							|  |  |  |                           DeprecationWarning, stacklevel=2) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 15:09:16 -08:00
										 |  |  |         self._bound_value = value | 
					
						
							|  |  |  |         super().__init__(value, loop=loop) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def release(self): | 
					
						
							|  |  |  |         if self._value >= self._bound_value: | 
					
						
							|  |  |  |             raise ValueError('BoundedSemaphore released too many times') | 
					
						
							|  |  |  |         super().release() |