32 lines
899 B
Python
32 lines
899 B
Python
"""Exclusive pipeline pause held until a synchronous or asynchronous operation ends."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Callable
|
|
|
|
|
|
class PipelinePause:
|
|
"""Suspend acquisition and restart intent; an explicit Stop cancels resumption."""
|
|
|
|
def __init__(self, valid: Callable[[], bool], release: Callable[[bool], None]) -> None:
|
|
self._valid = valid
|
|
self._release = release
|
|
self._finished = False
|
|
|
|
@property
|
|
def valid(self) -> bool:
|
|
return not self._finished and self._valid()
|
|
|
|
def finish(self, *, resume: bool = True) -> None:
|
|
if self._finished:
|
|
return
|
|
should_resume = resume and self.valid
|
|
self._finished = True
|
|
self._release(should_resume)
|
|
|
|
def __enter__(self) -> PipelinePause:
|
|
return self
|
|
|
|
def __exit__(self, *_exc: object) -> None:
|
|
self.finish()
|