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
(self)
| 362 | any(not w.cancelled() for w in (self._waiters or ()))) |
| 363 | |
| 364 | async def acquire(self): |
| 365 | """Acquire a semaphore. |
| 366 | |
| 367 | If the internal counter is larger than zero on entry, |
| 368 | decrement it by one and return True immediately. If it is |
| 369 | zero on entry, block, waiting until some other coroutine has |
| 370 | called release() to make it larger than 0, and then return |
| 371 | True. |
| 372 | """ |
| 373 | if not self.locked(): |
| 374 | self._value -= 1 |
| 375 | return True |
| 376 | |
| 377 | if self._waiters is None: |
| 378 | self._waiters = collections.deque() |
| 379 | fut = self._get_loop().create_future() |
| 380 | self._waiters.append(fut) |
| 381 | |
| 382 | # Finally block should be called before the CancelledError |
| 383 | # handling as we don't want CancelledError to call |
| 384 | # _wake_up_first() and attempt to wake up itself. |
| 385 | try: |
| 386 | try: |
| 387 | await fut |
| 388 | finally: |
| 389 | self._waiters.remove(fut) |
| 390 | except exceptions.CancelledError: |
| 391 | if not fut.cancelled(): |
| 392 | self._value += 1 |
| 393 | self._wake_up_next() |
| 394 | raise |
| 395 | |
| 396 | if self._value > 0: |
| 397 | self._wake_up_next() |
| 398 | return True |
| 399 | |
| 400 | def release(self): |
| 401 | """Release a semaphore, incrementing the internal counter by one. |
nothing calls this directly
no test coverage detected