Releases a Lock or Semaphore at the end of a "with" statement. with (yield semaphore.acquire()): pass # Now semaphore.release() has been called.
| 256 | |
| 257 | |
| 258 | class _ReleasingContextManager: |
| 259 | """Releases a Lock or Semaphore at the end of a "with" statement. |
| 260 | |
| 261 | with (yield semaphore.acquire()): |
| 262 | pass |
| 263 | |
| 264 | # Now semaphore.release() has been called. |
| 265 | """ |
| 266 | |
| 267 | def __init__(self, obj: Any) -> None: |
| 268 | self._obj = obj |
| 269 | |
| 270 | def __enter__(self) -> None: |
| 271 | pass |
| 272 | |
| 273 | def __exit__( |
| 274 | self, |
| 275 | exc_type: "Optional[Type[BaseException]]", |
| 276 | exc_val: Optional[BaseException], |
| 277 | exc_tb: Optional[types.TracebackType], |
| 278 | ) -> None: |
| 279 | self._obj.release() |
| 280 | |
| 281 | |
| 282 | class Semaphore(_TimeoutGarbageCollector): |