1
0
sqle/sqle/utl/future.py
2025-01-05 16:10:46 +09:00

27 lines
596 B
Python

from typing import Generic, TypeVar
from threading import Event
T = TypeVar("T")
class Future(Generic[T]):
_event: Event = Event()
_data : T|None = None
def get(self) -> T|None:
return self._data if self.filled() else None
def filled(self) -> bool:
return self._event.is_set()
class Promise(Future[T]):
def fill(self, data: T):
self._data = data
self._event.set()
return self
def is_filled(fut: Future[T]|None):
return fut.filled() if fut is not None else False
def is_filling(fut: Future[T]|None):
return (not fut.filled()) if fut is not None else False