| 615 | |
| 616 | |
| 617 | class TaskSemaphore: |
| 618 | def __init__(self, count): |
| 619 | """A semaphore for the purpose of limiting the number of tasks |
| 620 | |
| 621 | :param count: The size of semaphore |
| 622 | """ |
| 623 | self._semaphore = threading.Semaphore(count) |
| 624 | |
| 625 | def acquire(self, tag, blocking=True): |
| 626 | """Acquire the semaphore |
| 627 | |
| 628 | :param tag: A tag identifying what is acquiring the semaphore. Note |
| 629 | that this is not really needed to directly use this class but is |
| 630 | needed for API compatibility with the SlidingWindowSemaphore |
| 631 | implementation. |
| 632 | :param block: If True, block until it can be acquired. If False, |
| 633 | do not block and raise an exception if cannot be acquired. |
| 634 | |
| 635 | :returns: A token (can be None) to use when releasing the semaphore |
| 636 | """ |
| 637 | logger.debug("Acquiring %s", tag) |
| 638 | if not self._semaphore.acquire(blocking): |
| 639 | raise NoResourcesAvailable(f"Cannot acquire tag '{tag}'") |
| 640 | |
| 641 | def release(self, tag, acquire_token): |
| 642 | """Release the semaphore |
| 643 | |
| 644 | :param tag: A tag identifying what is releasing the semaphore |
| 645 | :param acquire_token: The token returned from when the semaphore was |
| 646 | acquired. Note that this is not really needed to directly use this |
| 647 | class but is needed for API compatibility with the |
| 648 | SlidingWindowSemaphore implementation. |
| 649 | """ |
| 650 | logger.debug(f"Releasing acquire {tag}/{acquire_token}") |
| 651 | self._semaphore.release() |
| 652 | |
| 653 | |
| 654 | class SlidingWindowSemaphore(TaskSemaphore): |
no outgoing calls