Decrement the counter. Returns an awaitable. Block if the counter is zero and wait for a `.release`. The awaitable raises `.TimeoutError` after the deadline.
(
self, timeout: Optional[Union[float, datetime.timedelta]] = None
)
| 412 | break |
| 413 | |
| 414 | def acquire( |
| 415 | self, timeout: Optional[Union[float, datetime.timedelta]] = None |
| 416 | ) -> Awaitable[_ReleasingContextManager]: |
| 417 | """Decrement the counter. Returns an awaitable. |
| 418 | |
| 419 | Block if the counter is zero and wait for a `.release`. The awaitable |
| 420 | raises `.TimeoutError` after the deadline. |
| 421 | """ |
| 422 | waiter = Future() # type: Future[_ReleasingContextManager] |
| 423 | if self._value > 0: |
| 424 | self._value -= 1 |
| 425 | waiter.set_result(_ReleasingContextManager(self)) |
| 426 | else: |
| 427 | self._waiters.append(waiter) |
| 428 | if timeout: |
| 429 | |
| 430 | def on_timeout() -> None: |
| 431 | if not waiter.done(): |
| 432 | waiter.set_exception(gen.TimeoutError()) |
| 433 | self._garbage_collect() |
| 434 | |
| 435 | io_loop = ioloop.IOLoop.current() |
| 436 | timeout_handle = io_loop.add_timeout(timeout, on_timeout) |
| 437 | waiter.add_done_callback( |
| 438 | lambda _: io_loop.remove_timeout(timeout_handle) |
| 439 | ) |
| 440 | return waiter |
| 441 | |
| 442 | def __enter__(self) -> None: |
| 443 | raise RuntimeError("Use 'async with' instead of 'with' for Semaphore") |